You are here

protected function InstallHelper::processArticle in Drupal 8

Same name and namespace in other branches
  1. 9 core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::processArticle()

Process article data into article node structure.

Parameters

array $data: Data of line that was read from the file.

string $langcode: Current language code.

Return value

array Data structured as an article node.

1 call to InstallHelper::processArticle()
InstallHelper::processContent in core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php
Process content into a structure that can be saved into Drupal.

File

core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php, line 528

Class

InstallHelper
Defines a helper class for importing default content.

Namespace

Drupal\demo_umami_content

Code

protected function processArticle(array $data, $langcode) {

  // Prepare content.
  $values = [
    'type' => 'article',
    'title' => $data['title'],
    'moderation_state' => 'published',
    'langcode' => 'en',
  ];

  // Fields mapping starts.
  // Set body field.
  if (!empty($data['body'])) {
    $body_path = $this->module_path . '/default_content/languages/' . $langcode . '/article_body/' . $data['body'];
    $body = file_get_contents($body_path);
    if ($body !== FALSE) {
      $values['body'] = [
        [
          'value' => $body,
          'format' => 'basic_html',
        ],
      ];
    }
  }

  // Set node alias if exists.
  if (!empty($data['slug'])) {
    $values['path'] = [
      [
        'alias' => '/' . $data['slug'],
      ],
    ];
  }

  // Save node alias
  $this
    ->saveNodePath($langcode, 'article', $data['id'], $data['slug']);

  // Set article author.
  if (!empty($data['author'])) {
    $values['uid'] = $this
      ->getUser($data['author']);
  }

  // Set field_media_image field.
  if (!empty($data['image_reference'])) {
    $values['field_media_image'] = [
      'target_id' => $this
        ->getMediaImageId($data['image_reference']),
    ];
  }

  // Set field_tags if exists.
  if (!empty($data['tags'])) {
    $values['field_tags'] = [];
    $tags = explode(',', $data['tags']);
    foreach ($tags as $tag_id) {
      if ($tid = $this
        ->getTermId('tags', $tag_id)) {
        $values['field_tags'][] = [
          'target_id' => $tid,
        ];
      }
    }
  }
  return $values;
}