You are here

public function ContentProcessor::processContentPane in GatherContent 8.4

Processing function for content panes.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Object of node.

string $local_field_id: ID of local Drupal field.

object $field: Object of GatherContent field.

bool $is_translatable: Indicator if node is translatable.

string $language: Language of translation if applicable.

array $files: Array of files fetched from GatherContent.

string $local_field_text_format: Text format setting for the local drupal field.

string $parent_field_type: Parent field type string to pass through field type in case of reference fields.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Core\Entity\EntityStorageException

\Exception

1 call to ContentProcessor::processContentPane()
ContentProcessor::createNode in src/Import/ContentProcess/ContentProcessor.php
Create a Drupal node filled with the properties of the GC item.

File

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

Class

ContentProcessor
The ContentProcessor sets the necessary fields of the entity.

Namespace

Drupal\gathercontent\Import\ContentProcess

Code

public function processContentPane(EntityInterface &$entity, $local_field_id, $field, $is_translatable, $language, array $files, $local_field_text_format, $parent_field_type = '') {
  if (empty(trim($field->value)) && empty($field->options) && $field->type !== 'files') {
    return;
  }
  $local_id_array = explode('||', $local_field_id);
  if (count($local_id_array) > 1) {
    $entityTypeManager = \Drupal::entityTypeManager();
    $field_info = FieldConfig::load($local_id_array[0]);
    $field_target_info = FieldConfig::load($local_id_array[1]);
    $field_name = $field_info
      ->getName();
    $entityStorage = $entityTypeManager
      ->getStorage($field_target_info
      ->getTargetEntityTypeId());
    $target_field_value = $entity
      ->getTranslation(Language::LANGCODE_DEFAULT)
      ->get($field_name)
      ->getValue();
    if (!isset($this->importedReferences[$local_id_array[0]])) {
      if (!empty($target_field_value)) {
        foreach ($target_field_value as $target) {
          $deleteEntity = $entityStorage
            ->load($target['target_id']);
          if ($deleteEntity) {
            $deleteEntity
              ->delete();
          }
        }
      }
      $this->importedReferences[$local_id_array[0]] = TRUE;
      $target_field_value = [];
    }
    array_shift($local_id_array);
    $to_import = TRUE;
    if (!empty($target_field_value)) {
      foreach ($target_field_value as $target) {
        $childEntity = $entityStorage
          ->loadByProperties([
          'id' => $target['target_id'],
          'type' => $field_target_info
            ->getTargetBundle(),
        ]);
        if (!empty($childEntity[$target['target_id']])) {
          $check_field_name = $field_target_info
            ->getName();
          $check_field_value = $childEntity[$target['target_id']]
            ->get($check_field_name)
            ->getValue();
          if ($is_translatable) {
            if (!$childEntity[$target['target_id']]
              ->hasTranslation($language)) {
              $childEntity[$target['target_id']]
                ->addTranslation($language);
            }
            if ($childEntity[$target['target_id']]
              ->hasTranslation($language)) {
              $check_field_value = $childEntity[$target['target_id']]
                ->getTranslation($language)
                ->get($check_field_name)
                ->getValue();
            }
          }
          if (count($local_id_array) > 1 || empty($check_field_value)) {
            $this
              ->processContentPane($childEntity[$target['target_id']], implode('||', $local_id_array), $field, $is_translatable, $language, $files, $local_field_text_format, $field_info
              ->getType());
            $childEntity[$target['target_id']]
              ->save();
            $to_import = FALSE;
          }
        }
      }
    }
    if ($to_import) {
      $childEntity = $entityStorage
        ->create([
        'type' => $field_target_info
          ->getTargetBundle(),
      ]);
      $this
        ->processContentPane($childEntity, implode('||', $local_id_array), $field, $is_translatable, $language, $files, $local_field_text_format, $field_info
        ->getType());
      $childEntity
        ->save();
      $target_field_value[] = [
        'target_id' => $childEntity
          ->id(),
        'target_revision_id' => $childEntity
          ->getRevisionId(),
      ];
    }
    $entity
      ->getTranslation(Language::LANGCODE_DEFAULT)
      ->set($field_name, $target_field_value);
  }
  else {
    $field_info = FieldConfig::load($local_field_id);
    if (!is_null($field_info)) {
      $is_translatable = $is_translatable && $field_info
        ->isTranslatable();
    }
    if ($local_field_id === 'title') {
      $target =& $entity;
      if ($is_translatable) {
        $target = $entity
          ->getTranslation($language);
        if (empty($field->value)) {
          throw new \Exception("Field '{$field->label}' must not be empty (it's a title field in a translatable item).");
        }
      }
      $target
        ->setTitle($field->value);
      return;
    }
    switch ($field->type) {
      case 'files':
        $this
          ->processFilesField($entity, $field_info, $field->id, $is_translatable, $language, $files);
        break;
      case 'choice_radio':
        $this
          ->processChoiceRadioField($entity, $field_info, $is_translatable, $language, $field->options);
        break;
      case 'choice_checkbox':
        $this
          ->processChoiceCheckboxField($entity, $field_info, $is_translatable, $language, $field->options);
        break;
      case 'section':
        $this
          ->processSectionField($entity, $field_info, $is_translatable, $language, $field, $local_field_text_format, $parent_field_type);
        break;
      default:
        $this
          ->processDefaultField($entity, $field_info, $is_translatable, $language, $field, $local_field_text_format, $parent_field_type);
        break;
    }
  }
}