You are here

protected function MappingSteps::filterFieldsRecursively in GatherContent 8.5

Same name and namespace in other branches
  1. 8.4 gathercontent_ui/src/Form/MappingEditSteps/MappingSteps.php \Drupal\gathercontent_ui\Form\MappingEditSteps\MappingSteps::filterFieldsRecursively()

Helper function.

Use for filtering only equivalent fields.

Parameters

object $gc_field: Type of field in GatherContent.

string $content_type: Name of Drupal content type.

string $entity_type: Name of Drupal Entity type.

array $nested_ids: Nested ID array.

string $bundle_label: Bundle label string.

Return value

array Associative array with equivalent fields.

1 call to MappingSteps::filterFieldsRecursively()
MappingSteps::filterFields in gathercontent_ui/src/Form/MappingEditSteps/MappingSteps.php
Wrapper function for filterFieldsRecursively.

File

gathercontent_ui/src/Form/MappingEditSteps/MappingSteps.php, line 335

Class

MappingSteps
Class MappingSteps.

Namespace

Drupal\gathercontent_ui\Form\MappingEditSteps

Code

protected function filterFieldsRecursively($gc_field, $content_type, $entity_type = 'node', array $nested_ids = [], $bundle_label = '') {
  $mapping_array = [
    'attachment' => [
      'file',
      'image',
      'entity_reference_revisions',
    ],
    'guidelines' => [
      'text_long',
      'entity_reference_revisions',
    ],
    'text' => [
      'text',
      'text_long',
      'text_with_summary',
      'string_long',
      'string',
      'email',
      'telephone',
      'date',
      'datetime',
      'entity_reference_revisions',
    ],
    'choice_radio' => [
      'string',
      'entity_reference',
      'entity_reference_revisions',
    ],
    'choice_checkbox' => [
      'list_string',
      'entity_reference',
      'entity_reference_revisions',
    ],
  ];
  $entityFieldManager = \Drupal::service('entity_field.manager');
  $entityTypeManager = \Drupal::entityTypeManager();
  $entityDefinition = $entityTypeManager
    ->getDefinition($entity_type);
  $titleKey = $entityDefinition
    ->getKey('label');

  /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $instances */
  $instances = $entityFieldManager
    ->getFieldDefinitions($entity_type, $content_type);
  $fields = [];

  // Fields.
  foreach ($instances as $instance) {
    if ($instance instanceof BaseFieldDefinition) {

      // Set label field.
      if ($gc_field->type === 'text' && $gc_field instanceof ElementText && $gc_field->metaData->isPlain && $titleKey == $instance
        ->getName()) {
        $fields[$titleKey] = $instance
          ->getLabel();
      }
      continue;
    }
    if (in_array($instance
      ->getType(), $mapping_array[$gc_field->type])) {

      // Constrains:
      // - do not map plain text (Drupal) to rich text (GC).
      // - do not map radios (GC) to text (Drupal),
      // if widget isn't provided by select_or_other module.
      // - do not map section (GC) to plain text (Drupal).
      // - map only taxonomy entity reference (Drupal) to radios
      // and checkboxes (GC).
      switch ($gc_field->type) {
        case 'text':
          if ((!isset($gc_field->metaData->isPlain) || !$gc_field->metaData->isPlain) && in_array($instance
            ->getType(), [
            'string',
            'string_long',
            'email',
            'telephone',
          ])) {
            continue 2;
          }
          break;
        case 'section':
          if (in_array($instance
            ->getType(), [
            'string',
            'string_long',
          ])) {
            continue 2;
          }
          break;
        case 'choice_radio':
        case 'choice_checkbox':
          if ($instance
            ->getType() !== 'entity_reference_revisions' && $instance
            ->getSetting('handler') !== 'default:taxonomy_term') {
            continue 2;
          }
          break;
      }
      $fieldStorageDefinition = $instance
        ->getFieldStorageDefinition();
      if ($instance
        ->getType() === 'entity_reference_revisions') {
        $settings = $instance
          ->getSetting('handler_settings');
        if (!empty($settings['target_bundles'])) {
          $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));
          }
          $target_type = $fieldStorageDefinition
            ->getSetting('target_type');
          $bundle_entity_type = $entityTypeManager
            ->getStorage($target_type)
            ->getEntityType()
            ->get('bundle_entity_type');
          $new_nested_ids = $nested_ids;
          $new_nested_ids[] = $instance
            ->id();
          foreach ($bundles as $bundle) {
            $new_bundle_label = (!empty($bundle_label) ? $bundle_label . ' - ' : '') . $instance
              ->getLabel();
            $bundle_name = $entityTypeManager
              ->getStorage($bundle_entity_type)
              ->load($bundle)
              ->label();
            $new_bundle_label .= ' (bundle: ' . $bundle_name . ')';
            $targetFields = $this
              ->filterFieldsRecursively($gc_field, $bundle, $target_type, $new_nested_ids, $new_bundle_label);
            if (!empty($targetFields)) {
              $fields = $fields + $targetFields;
            }
          }
        }
      }
      else {
        $key = $instance
          ->id();
        $instanceIsMultiple = $fieldStorageDefinition
          ->isMultiple();

        // GC field is a multi value field.
        if (!empty($gc_field->metaData->repeatable['isRepeatable']) && $gc_field->metaData->repeatable['isRepeatable']) {
          if (!$instanceIsMultiple) {
            continue;
          }
          $instanceCardinality = $fieldStorageDefinition
            ->getCardinality();

          // Has unlimited values.
          if ($gc_field->metaData->repeatable['limitEnabled'] === FALSE) {
            if ($instanceCardinality > 0) {
              continue;
            }
          }
          else {

            // Has limited values.
            if ($gc_field->metaData->repeatable['limit'] !== $instanceCardinality) {
              continue;
            }
          }
        }
        else {

          // Single values.
          if ($instanceIsMultiple && $gc_field->type === 'text') {
            continue;
          }
        }
        if (!empty($nested_ids)) {
          $new_nested_ids = $nested_ids;
          $new_nested_ids[] = $instance
            ->id();
          $key = implode('||', $new_nested_ids);
        }
        $fields[$key] = (!empty($bundle_label) ? $bundle_label . ' - ' : '') . $instance
          ->getLabel();
        if ($instance
          ->getType() === 'entity_reference' && $instance
          ->getSetting('handler') === 'default:taxonomy_term') {
          $mappingData = unserialize($this->mapping
            ->getData());
          if ($mappingData) {
            foreach ($mappingData as $groupName => $group) {
              $gcField = array_search($key, $group['elements']);
              if (empty($gcField)) {
                continue;
              }
              if (isset($group['language'])) {
                $this->entityReferenceFields[$key][$group['language']]['name'] = $gcField;
                $this->entityReferenceFields[$key][$group['language']]['tab'] = $groupName;
              }
              else {
                $this->entityReferenceFields[$key][LanguageInterface::LANGCODE_NOT_SPECIFIED]['name'] = $gcField;
                $this->entityReferenceFields[$key][LanguageInterface::LANGCODE_NOT_SPECIFIED]['tab'] = $groupName;
              }
            }
          }
          if (empty($this->entityReferenceFieldsOptions) || !in_array($key, $this->entityReferenceFieldsOptions)) {
            $this->entityReferenceFieldsOptions[] = $key;
          }
        }
      }
    }
  }
  return $fields;
}