function _paragraphs_collection_demo_create_image_paragraph

Helper function to create paragraph image.

Parameters

string $url: External URL of an image.

string $filename: Local file name of an image.

string $title: Image title.

string $alt: Image alt. If not set $title value will be used.

Return value

\Drupal\Core\Entity\EntityInterface Paragraphs image entity.

2 calls to _paragraphs_collection_demo_create_image_paragraph()
_paragraphs_collection_demo_create_demo_article in paragraphs_collection/modules/paragraphs_collection_demo/paragraphs_collection_demo.install
Create demo article example.
_paragraphs_collection_demo_create_grid_article in paragraphs_collection/modules/paragraphs_collection_demo/paragraphs_collection_demo.install
Create demo grid article example.

File

paragraphs_collection/modules/paragraphs_collection_demo/paragraphs_collection_demo.install, line 561
Installation hooks for paragraphs_collection_demo module.

Code

function _paragraphs_collection_demo_create_image_paragraph($filename, $title, $alt = NULL) {

  // Create image from module files.
  \Drupal::service('file_system')
    ->copy(\Drupal::service('extension.list.module')
    ->getPath('paragraphs_collection_demo') . '/files/' . $filename, 'public://' . $filename);
  $image = File::create([
    'uri' => 'public://' . $filename,
  ]);
  $image
    ->save();

  // Image paragraph type.
  $image_paragraph = Paragraph::create([
    'type' => 'image',
    'paragraphs_image' => [
      'target_id' => $image
        ->id(),
      'alt' => empty($alt) ? $title : $alt,
      'title' => $title,
    ],
  ]);
  $image_paragraph
    ->save();
  return $image_paragraph;
}