You are here

public function ContentProcessor::createNode in GatherContent 8.4

Create a Drupal node filled with the properties of the GC item.

File

src/Import/ContentProcess/ContentProcessor.php, line 99

Class

ContentProcessor
The ContentProcessor sets the necessary fields of the entity.

Namespace

Drupal\gathercontent\Import\ContentProcess

Code

public function createNode(Item $gc_item, ImportOptions $options, array $files) {
  $this->concatFieldValues = [];
  $mapping = MappingLoader::load($gc_item);
  $content_type = $mapping
    ->getContentType();
  $is_translatable = Importer::isContentTypeTranslatable($content_type);
  $mapping_data = unserialize($mapping
    ->getData());
  if (empty($mapping_data)) {
    throw new \Exception("Mapping data is empty.");
  }
  $first = reset($mapping_data);
  $langcode = isset($first['language']) ? $first['language'] : Language::LANGCODE_NOT_SPECIFIED;

  // Create a Drupal entity corresponding to GC item.
  $entity = NodeUpdateMethod::getDestinationNode($gc_item->id, $options
    ->getNodeUpdateMethod(), $content_type, $langcode);
  if ($entity === FALSE) {
    throw new \Exception("System error, please contact you administrator.");
  }

  // Create new revision according to the import options.
  if ($entity
    ->getEntityType()
    ->isRevisionable() && !$entity
    ->isNew() && $options
    ->getCreateNewRevision()) {
    $entity
      ->setNewRevision(TRUE);
    $entity
      ->setRevisionLogMessage('Created revision for node ID: ' . $entity
      ->id());
    $entity
      ->setRevisionCreationTime($this->time
      ->getRequestTime());
    $entity
      ->setRevisionUserId(\Drupal::currentUser()
      ->id());
  }
  $entity
    ->set('gc_id', $gc_item->id);
  $entity
    ->set('gc_mapping_id', $mapping
    ->id());
  $entity
    ->setOwnerId(\Drupal::currentUser()
    ->id());
  if ($entity
    ->isNew()) {
    $entity
      ->setPublished($options
      ->getPublish());
  }
  foreach ($gc_item->config as $pane) {
    $is_pane_translatable = $is_translatable && isset($mapping_data[$pane->id]['language']) && $mapping_data[$pane->id]['language'] != Language::LANGCODE_NOT_SPECIFIED;
    if ($is_pane_translatable) {
      $language = $mapping_data[$pane->id]['language'];
      if (!$entity
        ->hasTranslation($language)) {
        $entity
          ->addTranslation($language);
        if ($entity
          ->isNew()) {
          $entity
            ->getTranslation($language)
            ->setPublished($options
            ->getPublish());
        }
      }
    }
    else {
      $language = Language::LANGCODE_NOT_SPECIFIED;
    }
    foreach ($pane->elements as $field) {
      if (isset($mapping_data[$pane->id]['elements'][$field->id]) && !empty($mapping_data[$pane->id]['elements'][$field->id])) {
        $local_field_id = $mapping_data[$pane->id]['elements'][$field->id];
        $local_field_text_format = '';
        if (isset($mapping_data[$pane->id]['element_text_formats']) && !empty($mapping_data[$pane->id]['element_text_formats'][$field->id])) {
          $local_field_text_format = $mapping_data[$pane->id]['element_text_formats'][$field->id];
        }
        if (isset($mapping_data[$pane->id]['type']) && $mapping_data[$pane->id]['type'] === 'content' || !isset($mapping_data[$pane->id]['type'])) {
          $this
            ->processContentPane($entity, $local_field_id, $field, $is_pane_translatable, $language, $files, $local_field_text_format);
        }
        elseif (isset($mapping_data[$pane->id]['type']) && $mapping_data[$pane->id]['type'] === 'metatag') {
          $this
            ->processMetatagPane($entity, $local_field_id, $field, $mapping
            ->getContentType(), $is_pane_translatable, $language);
        }
      }
    }
  }
  if (empty($entity
    ->getTitle())) {
    $entity
      ->setTitle($gc_item->name);
  }
  return $entity;
}