Error message

  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1075 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).
  • Notice: Undefined property: stdClass::$preferred in _api_make_match_link() (line 1079 of /home/projects/api/www/sites/all/modules/api/api.formatting.inc).

paragraphs_collection_demo.install

Installation hooks for paragraphs_collection_demo module.

File

paragraphs_collection/modules/paragraphs_collection_demo/paragraphs_collection_demo.install
View source
<?php

/**
 * @file
 * Installation hooks for paragraphs_collection_demo module.
 */
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\paragraphs_library\Entity\LibraryItem;
use Drupal\user\Entity\User;

/**
 * Implements hook_install().
 */
function paragraphs_collection_demo_install() {
  _paragraphs_collection_demo_add_background_plugin_field();

  // Create and set the demo content on the front page.
  $node = _paragraphs_collection_demo_create_demo_article();
  \Drupal::configFactory()
    ->getEditable('system.site')
    ->set('page.front', '/node/' . $node
    ->id())
    ->save();
  _paragraphs_collection_demo_create_demo_article_1();
  _paragraphs_collection_demo_create_grid_article();
}

/**
 * Adds the background plugin field in the container paragraph type.
 */
function _paragraphs_collection_demo_add_background_plugin_field() {

  // Add the background field to the container form display.
  $container_form_display = EntityFormDisplay::load('paragraph.container.default');
  $content = $container_form_display
    ->get('content');
  $content['paragraphs_background_image'] = [
    'weight' => 1,
    'settings' => [
      'progress_indicator' => 'throbber',
      'preview_image_style' => 'thumbnail',
    ],
    'third_party_settings' => [],
    'type' => 'image_image',
    'region' => 'content',
  ];
  $container_form_display
    ->set('content', $content);
  $dependencies = $container_form_display
    ->get('dependencies');
  $dependencies['config'][] = 'field.field.paragraph.container.paragraphs_background_image';
  $dependencies['config'][] = 'image.style.thumbnail';
  $dependencies['module'][] = 'image';
  $container_form_display
    ->set('dependencies', $dependencies);
  $container_form_display
    ->save();

  // Add the background field to the container view display.
  $container_view_display = EntityViewDisplay::load('paragraph.container.default');
  $content = $container_view_display
    ->get('content');
  $content['paragraphs_background_image'] = [
    'weight' => 1,
    'label' => 'visually_hidden',
    'settings' => [
      'image_style' => '',
      'image_link' => '',
    ],
    'third_party_settings' => [],
    'type' => 'image',
    'region' => 'content',
  ];
  $container_view_display
    ->set('content', $content);
  $dependencies = $container_form_display
    ->get('dependencies');
  $dependencies['config'][] = 'field.field.paragraph.container.paragraphs_background_image';
  $dependencies['module'][] = 'image';
  $container_form_display
    ->set('dependencies', $dependencies);
  $container_view_display
    ->save();
  $container = ParagraphsType::load('container');
  $container
    ->set('behavior_plugins', [
    'background' => [
      'enabled' => TRUE,
      'background_image_field' => 'paragraphs_background_image',
    ],
    'style' => [
      'enabled' => TRUE,
      'groups' => [
        'general_group' => [
          'default' => '',
        ],
      ],
    ],
  ]);
  $container
    ->save();
}

/**
 * Create demo article example.
 *
 * @return \Drupal\Core\Entity\EntityInterface|static
 *   Returns node.
 */
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);
}

/**
 * Creates demo content.
 *
 * @return \Drupal\Core\Entity\EntityInterface
 *   Returns node.
 */
function _paragraphs_collection_demo_create_demo_article_1() {
  $paragraphs = [];

  // Create title paragraph.
  $title_paragraph = Paragraph::create([
    'type' => 'title',
    'paragraphs_title' => [
      'value' => 'The awesome new paragraph types',
    ],
  ]);
  $title_paragraph
    ->save();
  $paragraphs[] = $title_paragraph;

  // Create subtitle paragraph.
  $title_paragraph = Paragraph::create([
    'type' => 'subtitle',
    'paragraphs_subtitle' => [
      'value' => 'Exploring all new types',
    ],
  ]);
  $title_paragraph
    ->save();
  $paragraphs[] = $title_paragraph;

  // Create intro paragraph.
  $intro_paragraph = Paragraph::create([
    'type' => 'intro',
    'paragraphs_intro' => [
      'value' => '<p>Introduces powerful plugin system to attach behaviors to paragraph types.</p>',
      'format' => 'basic_html',
    ],
  ]);
  $intro_paragraph
    ->save();
  $paragraphs[] = $intro_paragraph;

  // Create text paragraph.
  $intro_paragraph = Paragraph::create([
    'type' => 'text',
    'paragraphs_text' => [
      'value' => '<p>Donec efficitur nisl diam, at finibus odio maximus ut. Ut diam nulla, tempus eget lacinia a, feugiat quis sapien. Mauris at erat pellentesque, malesuada mauris nec, vulputate leo. Nulla ultricies pharetra finibus. Integer placerat tempus orci, in bibendum lorem venenatis sit amet. Morbi viverra congue sapien, ac rhoncus sapien gravida sed. Donec malesuada turpis eu erat consectetur, sed semper elit finibus. Curabitur efficitur fringilla urna sollicitudin ultricies. Phasellus consectetur risus quis ex ultricies luctus.</p>
<p>In semper massa ante, vitae viverra metus sollicitudin varius. Mauris tempor lectus maximus nulla mattis maximus. In at volutpat diam. Nulla at velit ornare, sollicitudin neque sit amet, molestie massa. In et justo quis nibh mollis elementum. Fusce eget metus a tortor rutrum ornare quis a orci. Fusce volutpat sem ac mi pretium, eu rhoncus nisi convallis. Morbi sollicitudin tellus sit amet dui ultrices malesuada eget nec lacus. Vivamus pulvinar mattis vehicula. Cras dictum ut quam non pellentesque.</p>',
      'format' => 'basic_html',
    ],
  ]);
  $intro_paragraph
    ->save();
  $paragraphs[] = $intro_paragraph;

  // Create separator paragraph.
  $separator_paragraph = Paragraph::create([
    'type' => 'separator',
  ]);
  $separator_paragraph
    ->save();
  $paragraphs[] = $separator_paragraph;

  // Create quote paragraph.
  $quote_paragraph = Paragraph::create([
    'type' => 'quote',
    'paragraphs_quote_text' => [
      'value' => '<p>You must be the change you wish to see in the world.</p>',
      'format' => 'basic_html',
    ],
    'paragraphs_quote_author' => [
      'value' => 'Mahatma Gandhi',
    ],
  ]);
  $quote_paragraph
    ->save();
  $paragraphs[] = $quote_paragraph;

  // Create an image.
  $filename = 'nasa-43566.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();

  // Create image + text paragraph.
  $images_text_paragraph = Paragraph::create([
    'type' => 'image_text',
    'paragraphs_image' => [
      'target_id' => $image
        ->id(),
      'alt' => 'alternative text for an image',
      'title' => 'Image title',
    ],
    'paragraphs_text' => [
      'value' => '<p>This is an example text that goes along with the image in an Images + Text paragraph type.</p>',
      'format' => 'basic_html',
    ],
  ]);
  $images_text_paragraph
    ->save();
  $paragraphs[] = $images_text_paragraph;

  // Create user paragraph.
  $user = User::getAnonymousUser();
  $user_paragraph = Paragraph::create([
    'type' => 'user',
    'paragraphs_user' => [
      'target_id' => $user
        ->id(),
    ],
  ]);
  $user_paragraph
    ->save();
  $paragraphs[] = $user_paragraph;

  // Create footer paragraph.
  $footer_paragraph = Paragraph::create([
    'type' => 'footer',
    'paragraphs_footer' => [
      'value' => '<p>So what awesome solution YOU are going to build with paragraphs?</p>',
      'format' => 'basic_html',
    ],
  ]);
  $footer_paragraph
    ->save();
  $paragraphs[] = $footer_paragraph;

  // Create the demo node.
  return _paragraphs_collection_demo_create_node('Paragraphs Collection Fundamental Types', $paragraphs);
}

/**
 * Create demo grid article example.
 *
 * @return \Drupal\Core\Entity\EntityInterface
 *   Returns node.
 */
function _paragraphs_collection_demo_create_grid_article() {
  $paragraphs = [];

  // PARAGRAPH DEMO ITEMS: equal layout example with 3 items.
  $paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<h2>Equal columns example with 3 and 4 items</h2>');
  $grid_paragraphs = [];
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>We offer various grid layouts in demo. This one is showing three items all with the same size.</p>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>No matter how much items you add they will all be in the same size.</p>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>I am just another item so three equal cols example can be a reality.</p>');
  $paragraphs[] = _paragraphs_collection_demo_create_grid_paragraph($grid_paragraphs, 'paragraphs_collection_demo_equal_columns');

  // PARAGRAPH DEMO ITEMS: equal layout example with 4 items.
  $grid_paragraphs = [];
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>Professor, make a woman out of me. Good man. Nixon\'s pro-war and pro-family. Man, I\'m sore all over. I feel like I just went ten rounds with mighty Thor. I haven\'t felt much of anything since my guinea pig died.</p>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>With a warning label this big, you know they gotta be fun!</p>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>I barely knew Philip, but as a clergyman I have no problem telling his most intimate friends all about him.</p>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_image_paragraph('bill-williams-1806.jpg', 'Hummingbird');
  $paragraphs[] = _paragraphs_collection_demo_create_grid_paragraph($grid_paragraphs, 'paragraphs_collection_demo_equal_columns');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>You can add as much items as you like and they are all going to be equal width. Enough with equals lets show some fixed stuff now.</p>');

  // PARAGRAPH DEMO ITEMS: 1 - 2 layout example.
  $paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<h2>How about two columns layout in 1/3 of a steps?</h2>');
  $grid_paragraphs = [];
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>Our first example is showing two text items in 1 - 2 grid layout.</p>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>We don\'t have a brig. You lived before you met me?! Fetal stemcells, aren\'t those controversial? Leela, are you alright? You got wanged on the head. Well, thanks to the Internet, I\'m now bored with sex. Is there a place on the web that panders to my lust for violence?</p><ol><li>I could if you hadn\'t turned on the light and shut off my stereo.</li><li>Oh dear! She\'s stuck in an infinite loop, and he\'s an idiot! Well, that\'s love for you.</li><li>Soothe us with sweet lies.</li></ol>');
  $paragraphs[] = _paragraphs_collection_demo_create_grid_paragraph($grid_paragraphs, 'paragraphs_collection_demo_1_2_column');

  // PARAGRAPH DEMO ITEMS: 2 - 1 layout example.
  $grid_paragraphs = [];
  $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>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_image_paragraph('nasa-43566.jpg', 'Planet Earth');
  $paragraphs[] = _paragraphs_collection_demo_create_grid_paragraph($grid_paragraphs, 'paragraphs_collection_demo_2_1_column');

  // PARAGRAPH DEMO ITEMS: 1 - 2 - 1 layout example.
  $paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<h2>Adding 25% width step for grid more mixin options</h2>');
  $grid_paragraphs = [];

  // Slider paragraph with images.
  $images[] = _paragraphs_collection_demo_create_image_paragraph('ales-krivec-1881.jpg', 'Lake and Mountain');
  $images[] = _paragraphs_collection_demo_create_image_paragraph('ales-krivec-434.jpg', 'Green Lake and Mountain');
  $images[] = _paragraphs_collection_demo_create_image_paragraph('joshua-k-jackson-121873.jpg', 'Cathedral From Top');
  $images[] = _paragraphs_collection_demo_create_image_paragraph('frantzou-fleurine-16979.jpg', 'The Waves');
  $images[] = _paragraphs_collection_demo_create_image_paragraph('annie-spratt-114046.jpg', 'Giving is Caring');
  $slider_paragraph = Paragraph::create([
    'type' => 'slider',
    'field_slides' => $images,
  ]);
  $slider_paragraph
    ->setBehaviorSettings('slider', [
    'slick_slider' => 'default',
  ]);
  $slider_paragraph
    ->save();
  $grid_paragraphs[] = $slider_paragraph;

  // Other two text items.
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>25 - 50 - 25 example, and yeah you can put what ever you want for grid items, text, images or complex components.</p>');
  $grid_paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<p>Every grid brings us closer to the next content editor nirvana:)</p>');
  $paragraphs[] = _paragraphs_collection_demo_create_grid_paragraph($grid_paragraphs, 'paragraphs_collection_demo_1_2_1_column');
  $paragraphs[] = _paragraphs_collection_demo_create_text_paragraph('<h2>What next?</h2><p>As the last info our demo grid layout rules are responsive out of the box. For small screen size we resize all grid items to 100% width - just try it your self, resize the screen and test responsive behaviors. It\'s also easy to add more complex responsive rules, CSS grid rules are defined by frontend developers and they have full control of grid layouts rules they want to use.</p>');
  return _paragraphs_collection_demo_create_node('More Grid Examples', $paragraphs);
}

/**
 * Create demo node of a type paragraphed_content_demo.
 *
 * @param string $title
 *   Node title.
 * @param array $paragraphs
 *   Array of paragraphs entites.
 *
 * @return \Drupal\Core\Entity\EntityInterface
 *   Saved node entity.
 */
function _paragraphs_collection_demo_create_node($title, array $paragraphs) {
  $node = Node::create([
    'type' => 'paragraphed_content_demo',
    'title' => $title,
    'langcode' => 'en',
    'uid' => '0',
    'status' => 1,
    'field_paragraphs_demo' => $paragraphs,
  ]);
  $node
    ->save();
  return $node;
}

/**
 * Helper function to create paragraph text.
 *
 * @param string $text
 *   Paragraph text.
 * @param string $format
 *   Text format.
 *
 * @return \Drupal\Core\Entity\EntityInterface
 *   Paragraphs text entity.
 */
function _paragraphs_collection_demo_create_text_paragraph($text, $format = 'basic_html') {
  $text_paragraph = Paragraph::create([
    'type' => 'text',
    'paragraphs_text' => [
      'value' => $text,
      'format' => $format,
    ],
  ]);
  $text_paragraph
    ->save();
  return $text_paragraph;
}

/**
 * Helper function to create paragraph grid.
 *
 * @param array $items
 *   Array of paragraphs items.
 * @param string $layout
 *   Layout name.
 *
 * @return \Drupal\Core\Entity\EntityInterface
 *   Paragraphs grid entity.
 */
function _paragraphs_collection_demo_create_grid_paragraph(array $items, $layout) {
  $grid_paragraph = Paragraph::create([
    'type' => 'grid',
    'paragraphs_container_paragraphs' => $items,
  ]);
  $grid_paragraph
    ->setBehaviorSettings('grid_layout', [
    'layout' => $layout,
  ]);
  $grid_paragraph
    ->save();
  return $grid_paragraph;
}

/**
 * Helper function to create paragraph image.
 *
 * @param string $url
 *   External URL of an image.
 * @param string $filename
 *   Local file name of an image.
 * @param string $title
 *   Image title.
 * @param string $alt
 *   Image alt. If not set $title value will be used.
 *
 * @return \Drupal\Core\Entity\EntityInterface
 *   Paragraphs image entity.
 */
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;
}

/**
 * Helper function to create accordion paragraph.
 *
 * @param array $items
 *   Accordion items.
 *
 * @return \Drupal\Core\Entity\EntityInterface
 *   Paragraphs accordion entity.
 */
function _paragraphs_collection_demo_create_accordion_paragraph(array $items) {

  // Accordion paragraph.
  $accordion_paragraph = Paragraph::create([
    'type' => 'accordion',
    'paragraphs_accordion_paragraphs' => $items,
  ]);
  $accordion_paragraph
    ->save();
  return $accordion_paragraph;
}

Functions

Namesort descending Description
paragraphs_collection_demo_install Implements hook_install().
_paragraphs_collection_demo_add_background_plugin_field Adds the background plugin field in the container paragraph type.
_paragraphs_collection_demo_create_accordion_paragraph Helper function to create accordion paragraph.
_paragraphs_collection_demo_create_demo_article Create demo article example.
_paragraphs_collection_demo_create_demo_article_1 Creates demo content.
_paragraphs_collection_demo_create_grid_article Create demo grid article example.
_paragraphs_collection_demo_create_grid_paragraph Helper function to create paragraph grid.
_paragraphs_collection_demo_create_image_paragraph Helper function to create paragraph image.
_paragraphs_collection_demo_create_node Create demo node of a type paragraphed_content_demo.
_paragraphs_collection_demo_create_text_paragraph Helper function to create paragraph text.