function _paragraphs_collection_demo_create_demo_article

Create demo article example.

Return value

\Drupal\Core\Entity\EntityInterface|static Returns node.

1 call to _paragraphs_collection_demo_create_demo_article()
paragraphs_collection_demo_install in paragraphs_collection/modules/paragraphs_collection_demo/paragraphs_collection_demo.install
Implements hook_install().

File

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

Code

function _paragraphs_collection_demo_create_demo_article() {
  $paragraphs = [];

  // PARAGRAPH DEMO ITEM: style.
  // Text content for style paragraph.
  $style_text_paragraph = _paragraphs_collection_demo_create_text_paragraph('<h2>Styles behavior</h2><p>Allows us to apply styles to paragraphs.</p>');

  // Style paragraph.
  $style_paragraph = Paragraph::create([
    'title' => 'Style demo paragraph',
    'type' => 'container',
    'paragraphs_container_paragraphs' => [
      $style_text_paragraph,
    ],
  ]);
  $style_paragraph
    ->setBehaviorSettings('style', [
    'styles' => [
      'general_group' => 'paragraphs-green',
    ],
  ]);
  $style_paragraph
    ->save();
  $paragraphs[] = $style_paragraph;

  // PARAGRAPH DEMO ITEM: background image.
  // Image to be used for the background.
  $filename = 'kazuend-32607.jpg';
  \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();

  // Text content for background image paragraph.
  $background_image_text_paragraph = _paragraphs_collection_demo_create_text_paragraph('<h2>Background image behavior</h2><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>');

  // Background image paragraph.
  $background_image_paragraph = Paragraph::create([
    'title' => 'Background image demo paragraph',
    'type' => 'container',
    'paragraphs_container_paragraphs' => [
      $background_image_text_paragraph,
    ],
    'paragraphs_background_image' => [
      'target_id' => $image
        ->id(),
      'alt' => 'Druplicon',
      'title' => 'Druplicon image',
    ],
  ]);
  $background_image_paragraph
    ->save();
  $paragraphs[] = $background_image_paragraph;

  // PARAGRAPH DEMO ITEM: slider.
  // Text content for slides.
  $slider_text_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<h2>Slides behavior</h2><p>Convert paragraphs into slides. This is the first slide using our slider plugin.</p>');
  $slider_text_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<h2>More slides!</h2><p>This is the second slide using our slider plugin.</p>');

  // Slider paragraph.
  $slider_paragraph = Paragraph::create([
    'title' => 'Demo Paragraph',
    'type' => 'slider',
    'field_slides' => $slider_text_paragraphs,
  ]);
  $slider_paragraph
    ->setBehaviorSettings('slider', [
    'slick_slider' => 'default',
  ]);
  $slider_paragraph
    ->save();
  $paragraphs[] = $slider_paragraph;

  // PARAGRAPH DEMO ITEM: infinite slider.
  // Creating demo content with the following 5 images:
  $slider_infinite_images = [
    'image-1.png',
    'image-2.png',
    'image-3.png',
    'image-4.png',
    'image-5.png',
  ];

  // Load images and create an image-paragraph.
  $slider_infinite_paragraphs = [];
  foreach ($slider_infinite_images as $index => $slider_infinite_image_url) {
    $slider_infinite_paragraphs[] = _paragraphs_collection_demo_create_image_paragraph($slider_infinite_image_url, 'Demo Image ' . ($index + 1), 'Alternative text for the demo image ' . ($index + 1));
  }

  // Add the single items to a new infinite slider paragraph.
  $slider_infinite_paragraph = Paragraph::create([
    'title' => 'Demo infinite slider Paragraph',
    'type' => 'slider_infinite',
    'field_slides' => $slider_infinite_paragraphs,
  ]);
  $slider_infinite_paragraph
    ->setBehaviorSettings('slider', [
    'slick_slider' => 'slider_infinite',
  ]);
  $slider_infinite_paragraph
    ->save();
  $paragraphs[] = $slider_infinite_paragraph;

  // PARAGRAPH DEMO ITEM: grid.
  // Text content for grid.
  $grid_text_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<h2>Grids behavior</h2><p>Display your paragraphs in grids. This text should be on the left side.</p>');
  $grid_text_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<h2>Grids everywhere!</h2><p>This text should be on the right side.</p>');

  // Grid paragraphs.
  $paragraphs[] = _paragraphs_collection_demo_create_grid_paragraph($grid_text_paragraphs, 'paragraphs_collection_demo_equal_columns');

  // PARAGRAPH DEMO ITEM: block.
  // Block paragraph.
  $block_paragraph = Paragraph::create([
    'title' => 'Block paragraph',
    'type' => 'block',
    'field_block' => [
      'plugin_id' => 'views_block:paragraphs_collection_demo_content-block_1',
      'settings' => [
        'label_display' => FALSE,
      ],
    ],
  ]);
  $block_paragraph
    ->save();
  $paragraphs[] = $block_paragraph;

  // Update block field settings.
  $field_config = FieldConfig::load('paragraph.block.field_block');
  $settings = [
    'views_block:paragraphs_collection_demo_content-block_1' => 'views_block:paragraphs_collection_demo_content-block_1',
  ];
  $selection_settings = $field_config
    ->getSettings()['selection_settings'];
  $selection_settings['plugin_ids'] = array_unique(array_merge($selection_settings['plugin_ids'], $settings));
  $field_config
    ->setSetting('selection_settings', $selection_settings);
  $field_config
    ->save();

  // PARAGRAPH DEMO ITEM: library items.
  $library_text_paragraph = _paragraphs_collection_demo_create_text_paragraph('This is content from the library. We can reuse it multiple times without duplicating it.', 'plain_text');
  $library_item = LibraryItem::create([
    'label' => 'Library item',
    'paragraphs' => [
      'target_id' => $library_text_paragraph
        ->id(),
      'target_revision_id' => $library_text_paragraph->revision_id->value,
    ],
  ]);
  $library_item
    ->save();
  $from_library = Paragraph::create([
    'type' => 'from_library',
    'field_reusable_paragraph' => [
      'target_id' => $library_item
        ->id(),
      'target_revision_id' => $library_item
        ->getRevisionId(),
    ],
  ]);
  $from_library
    ->save();
  $paragraphs[] = $from_library;

  // PARAGRAPH DEMO ITEM: accordion.
  $accordion_children = [];
  $accordion_children[] = _paragraphs_collection_demo_create_text_paragraph('Accordion title #1 - Simple');
  $text = '<p>Paragraphs introduced a powerful plugin system to attach behaviors to paragraph types.</p>
    <p>This project is a collection of EXPERIMENTS to provide plugins for a rich variety of paragraph types.<br />
    These plugins are available or in progress:</p>
    <ul>
      <li>Grids</li>
      <li>Sliders</li>
      <li>Accordion</li>
      <li>Anchor</li>
      <li>Style selection (a set of predefined styles)</li>
      <li>Background</li>
      <li>Lock editing</li>
      <li>Visibility per language</li>
    </ul>';
  $accordion_children[] = _paragraphs_collection_demo_create_text_paragraph($text);
  $accordion_children[] = _paragraphs_collection_demo_create_text_paragraph('Accordion title #2 - Image');
  $accordion_children[] = _paragraphs_collection_demo_create_image_paragraph('nasa-43566.jpg', 'Planet Earth');
  $accordion_children[] = _paragraphs_collection_demo_create_text_paragraph('Accordion title #3 - Grid');
  $grid_paragraphs = [];
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>This example is showing two text items in 1 - 2 grid layout.</p>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>"<strong>Space: the final frontier.</strong><br/>These are the voyages of the starship Enterprise. Its five-year mission: to explore strange new worlds; to seek out new life and new civilisations; to boldly go where no man has gone before." - <em>Captain James T. Kirk</em></p>');
  $accordion_children[] = _paragraphs_collection_demo_create_grid_paragraph($grid_paragraphs, 'paragraphs_collection_demo_1_2_column');
  $paragraphs[] = _paragraphs_collection_demo_create_accordion_paragraph($accordion_children);

  // PARAGRAPH DEMO ITEM: language.
  // Enable the Language plugin for the 'text' paragraphs type from the
  // paragraphs_demo module.

  /** @var ParagraphsType $paragraphs_type */
  $paragraphs_type = paragraphs_type_load('text');
  $paragraphs_type
    ->getBehaviorPlugin('language')
    ->setConfiguration([
    'enabled' => TRUE,
  ]);
  $paragraphs_type
    ->save();

  // Language paragraph.
  $language_paragraph = _paragraphs_collection_demo_create_text_paragraph('<h2>Language behavior</h2><p>This paragraphs is only visible in English.</p>');
  $language_paragraph
    ->setBehaviorSettings('language', [
    'container' => [
      'visibility' => 'show',
      'languages' => [
        'en',
      ],
    ],
  ]);
  $language_paragraph
    ->save();
  $paragraphs[] = $language_paragraph;

  // Create the demo node.
  return _paragraphs_collection_demo_create_node('Paragraphs Collection Demo Article!', $paragraphs);
}