You are here

protected function MappingCreator::processFields in GatherContent 8.5

Process the fields.

Parameters

array $fields: Fields list.

\Drupal\Core\Language\LanguageInterface $language: Language object.

array $group: Group array.

array $mappingData: Mapping data array.

string $groupUuid: Group's UUID.

string $parentFieldId: Parent field's ID.

string $parentFieldLabel: Parent field's Label.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to MappingCreator::processFields()
MappingCreator::generateMapping in gathercontent_upload/src/Export/MappingCreator.php
Generates template, mapping and migration definition for given entity type and bundle.

File

gathercontent_upload/src/Export/MappingCreator.php, line 291

Class

MappingCreator
Class for handling import/update logic from GatherContent to Drupal.

Namespace

Drupal\gathercontent_upload\Export

Code

protected function processFields(array $fields, LanguageInterface $language, array &$group, array &$mappingData, string $groupUuid, string $parentFieldId = '', string $parentFieldLabel = '') {
  foreach ($fields as $field) {
    if ($field instanceof BaseFieldDefinition) {
      continue;
    }

    /** @var \Drupal\field\Entity\FieldConfig $field */
    if (empty(static::FIELD_COMBINATIONS[$field
      ->getType()]) && $field
      ->getType() !== 'entity_reference' && $field
      ->getType() !== 'entity_reference_revisions') {
      continue;
    }
    $fieldType = 'text';
    $metadata = [
      'is_plain' => FALSE,
    ];
    if (!empty(static::FIELD_COMBINATIONS[$field
      ->getType()])) {
      $fieldType = static::FIELD_COMBINATIONS[$field
        ->getType()];
    }
    if ($fieldType === 'plain') {
      $fieldType = 'text';
      $metadata = [
        'is_plain' => TRUE,
      ];
    }
    if ($fieldType === 'text') {
      $fieldStorageDefinition = $field
        ->getFieldStorageDefinition();
      $metadata['repeatable'] = [
        'isRepeatable' => FALSE,
        'limitEnabled' => FALSE,
        'limit' => 1,
      ];
      if ($fieldStorageDefinition
        ->isMultiple()) {
        $metadata['repeatable']['isRepeatable'] = TRUE;
        $fieldCardinality = $fieldStorageDefinition
          ->getCardinality();
        if ($fieldCardinality > 1) {
          $metadata['repeatable']['limitEnabled'] = TRUE;
          $metadata['repeatable']['limit'] = $fieldCardinality;
        }
      }
    }
    if ($field
      ->getType() === 'entity_reference') {
      if ($field
        ->getSetting('handler') !== 'default:taxonomy_term') {
        continue;
      }
      $fieldType = 'choice_checkbox';
      if (!$field
        ->getFieldStorageDefinition()
        ->isMultiple()) {
        $fieldType = 'choice_radio';
      }
      $termStorage = $this->entityTypeManager
        ->getStorage('taxonomy_term');
      $values = [
        'langcode' => $language
          ->getId(),
      ];
      $settings = $field
        ->getSetting('handler_settings');
      if (!empty($settings['target_bundles'])) {
        $values['vid'] = $settings['target_bundles'];
      }
      $terms = $termStorage
        ->loadByProperties($values);
      $options = [];
      foreach ($terms as $term) {
        $uuid = $this->uuidService
          ->generate();
        $options[] = [
          'optionId' => $uuid,
          'label' => $term
            ->label(),
        ];
        $optionIds = $term
          ->get('gathercontent_option_ids')
          ->getValue();
        $mappedValues = array_map(function ($array) {
          return $array['value'];
        }, $optionIds);
        if (!in_array($uuid, $mappedValues)) {
          $term->gathercontent_option_ids
            ->appendItem($uuid);
        }
        $term
          ->save();
      }
      $metadata = [
        'choice_fields' => [
          'options' => $options,
        ],
      ];
    }
    if ($field
      ->getType() === 'entity_reference_revisions') {
      $settings = $field
        ->getSetting('handler_settings');
      if (empty($settings['target_bundles'])) {
        continue;
      }
      $bundles = $settings['target_bundles'];
      if (!empty($settings['negate']) && !empty($settings['target_bundles_drag_drop'])) {
        $negated_bundles = array_filter($settings['target_bundles_drag_drop'], function ($v) {
          return !$v['enabled'];
        });
        $bundles = array_combine(array_keys($negated_bundles), array_keys($negated_bundles));
      }
      $targetType = $field
        ->getFieldStorageDefinition()
        ->getSetting('target_type');
      foreach ($bundles as $bundle) {
        $childFields = $this->entityFieldManager
          ->getFieldDefinitions($targetType, $bundle);
        $this
          ->processFields($childFields, $language, $group, $mappingData, $groupUuid, $field
          ->id(), (string) $field
          ->getLabel());
      }
      continue;
    }
    $fieldUuid = $this->uuidService
      ->generate();
    $fieldLabel = (string) $field
      ->getLabel();
    if (!empty($parentFieldLabel)) {
      $fieldLabel = $parentFieldLabel . ' ' . $fieldLabel;
    }
    $group['fields'][] = [
      'uuid' => $fieldUuid,
      'field_type' => $fieldType,
      'label' => $fieldLabel,
      'metadata' => $metadata,
    ];
    $fieldId = $field
      ->id();
    if (!empty($parentFieldId)) {
      $fieldId = $parentFieldId . '||' . $fieldId;
    }
    $mappingData[$groupUuid]['elements'][$fieldUuid] = $fieldId;
  }
}