<?php
/**
 * @file
 * Installation hooks for paragraphs_demo module.
 */

use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs_library\Entity\LibraryItem;
use Drupal\workflows\Entity\Workflow;

/**
 * Implements hook_install().
 */
function paragraphs_demo_install() {
  if (version_compare(\Drupal::VERSION, '8.6.99', '<=')) {
    // Ensure the translation fields are created in the database.
    \Drupal::service('entity.definition_update_manager')->applyUpdates();
  }

  // Create three paragraphs to structure the content.
  $paragraph = Paragraph::create([
    'type' => 'text',
    'field_text_demo' => [
      'value' => '<h2>Paragraphs is the new way of content creation!</h2>
      <p>It allows you — Site Builders — to make things cleaner so that you can give more editing power to your end-users.
      Instead of putting all their content in one WYSIWYG body field including images and videos, end-users can now choose on-the-fly between pre-defined Paragraph Types independent from one another. Paragraph Types can be anything you want from a simple text block or image to a complex and configurable slideshow.</p>',
      'format' => 'basic_html',
    ],
  ]);
  $paragraph->save();

  $paragraph2 = Paragraph::create([
    'type' => 'text',
    'field_text_demo' => [
      'value' => '<p>This demo creates some default Paragraph types from which you can easily create some content (Nested Paragraph, Text, Image + Text, Text + Image, Image and User). It also includes some basic styling and assures that the content is responsive on any device.</p>',
      'format' => 'basic_html',
    ],
  ]);
  $paragraph2->save();

  $paragraph3 = Paragraph::create([
    'type' => 'text',
    'field_text_demo' => [
      'value' => '<p>Apart from the included Paragraph types, you can create your own simply by going to Structure -> Paragraphs types.</p>',
      'format' => 'basic_html',
    ],
  ]);
  $paragraph3->save();

  $paragraph4 = Paragraph::create([
    'type' => 'text',
    'field_text_demo' => [
      'value' => '<p>A search api example can be found <a href="/paragraphs_search">here</a></p>',
      'format' => 'basic_html',
    ],
  ]);
  $paragraph4->save();

  $paragraph5 = Paragraph::create([
    'type' => 'nested_paragraph',
    'field_paragraphs_demo' => $paragraph4,
  ]);
  $paragraph5->save();

  // PARAGRAPH DEMO ITEM: library items.
  $library_text_paragraph = Paragraph::create([
    'type' => 'text',
    'field_text_demo' => [
      'value' => 'This is content from the library. We can reuse it multiple times without duplicating it.',
      'format' => 'plain_text',
    ],
  ]);
  $library_text_paragraph->save();

  $library_item = LibraryItem::create([
    'label' => 'Library item',
    'paragraphs' => [
      $library_text_paragraph,
    ],
  ]);
  $library_item->save();

  $from_library = Paragraph::create([
    'type' => 'from_library',
    'field_reusable_paragraph' => [
      $library_item,
    ],
  ]);
  $from_library->save();

  // Add demo content with four paragraphs.
  $node = Node::create([
    'type' => 'paragraphed_content_demo',
    'title' => 'Welcome to the Paragraphs Demo module!',
    'langcode' => 'en',
    'uid' => '0',
    'status' => 1,
    'field_paragraphs_demo' => [
      $paragraph,
      $paragraph2,
      $paragraph3,
      $paragraph5,
      $from_library,
    ],
  ]);
  $node->save();
  // Set the node as the front page.
  \Drupal::configFactory()->getEditable('system.site')->set('page.front', '/node/' . $node->id())->save();

  if (\Drupal::getContainer()->has('search_api.post_request_indexing')) {
    \Drupal::service('search_api.post_request_indexing')->destruct();
  }

  if ($workflow = Workflow::load('editorial')) {
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'paragraphed_content_demo');
    $workflow->save();
  }

}
