Adds an icon to a paragraphs type.
string $paragraphs_type: Machine name of the paragraph type to add the icon to.
\Drupal\file\Entity\File The file entity used as the icon.
protected function addParagraphsTypeIcon($paragraphs_type) {
// Get an image.
$image_files = $this
->getTestFiles('image');
$uri = current($image_files)->uri;
// Create a copy of the image, so that multiple file entities don't
// reference the same file.
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$copy_uri = $file_system
->copy($uri, 'public://' . $file_system
->basename($uri));
// Create a new file entity.
$file_entity = File::create([
'uri' => $copy_uri,
]);
$file_entity
->save();
// Add the file entity to the paragraphs type as its icon.
$paragraphs_type_entity = ParagraphsType::load($paragraphs_type);
$paragraphs_type_entity
->set('icon_uuid', $file_entity
->uuid());
$paragraphs_type_entity
->save();
return $file_entity;
}