You are here

protected function MappingEditForm::filterFields in GatherContent 8.3

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.

Return value

array Associative array with equivalent fields.

1 call to MappingEditForm::filterFields()
MappingEditForm::form in src/Form/MappingEditForm.php
Gets the actual form array to be built.

File

src/Form/MappingEditForm.php, line 553

Class

MappingEditForm
Class MappingEditForm.

Namespace

Drupal\gathercontent\Form

Code

protected function filterFields($gc_field, $content_type) {
  $mapping_array = [
    'files' => [
      'file',
      'image',
    ],
    'section' => [
      'text_long',
    ],
    'text' => [
      'text',
      'text_long',
      'text_with_summary',
      'string_long',
      'string',
      'email',
      'telephone',
      'date',
      'datetime',
    ],
    'choice_radio' => [
      'string',
      'entity_reference',
    ],
    'choice_checkbox' => [
      'list_string',
      'entity_reference',
    ],
  ];

  /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $instances */
  $instances = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('node', $content_type);
  $fields = [];

  // Fields.
  foreach ($instances as $name => $instance) {
    if (substr_compare($name, 'field', 0, 5) != 0 && !in_array($name, [
      'body',
    ])) {
      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 (!$gc_field->plain_text && 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
            ->getSetting('handler') !== 'default:taxonomy_term') {
            continue 2;
          }
          break;
      }
      $fields[$instance
        ->getName()] = $instance
        ->getLabel();
    }
  }
  if ($gc_field->type === 'text' && $gc_field->plain_text) {
    $fields['title'] = 'Title';
  }
  return $fields;
}