You are here

public function GathercontentMappingEditForm::filter_fields in GatherContent 8

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 GathercontentMappingEditForm::filter_fields()
GathercontentMappingEditForm::form in src/Form/GathercontentMappingEditForm.php
Gets the actual form array to be built.

File

src/Form/GathercontentMappingEditForm.php, line 197

Class

GathercontentMappingEditForm
Class GathercontentMappingEditForm.

Namespace

Drupal\gathercontent\Form

Code

public function filter_fields($gc_field, $content_type) {
  $mapping_array = array(
    'files' => array(
      'file',
      'image',
    ),
    'section' => array(
      'text_long',
    ),
    'text' => array(
      'text',
      'text_long',
      'text_with_summary',
      'string_long',
      'string',
    ),
    'choice_radio' => array(
      'text',
    ),
    'choice_checkbox' => array(
      'list_text',
    ),
  );
  $instances = $this->entityManager
    ->getFieldDefinitions('node', $content_type);
  $fields = array();

  // Fields.
  foreach ($instances as $name => $instance) {
    if (substr_compare($name, 'field', 0, 5) != 0 && !in_array($name, array(
      '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).
      switch ($gc_field->type) {
        case 'text':
          if ($gc_field->plain_text && !in_array($instance
            ->getType(), array(
            'string',
            'string_long',
          ))) {
            continue 2;
          }
          break;
        case 'choise_radio':
          if ($instance['widget']['module'] !== 'select_or_other') {
            continue 2;
          }
          break;
        case 'section':
          if (in_array($instance
            ->getType(), array(
            'string',
            'string_long',
          ))) {
            continue 2;
          }
          break;
      }
      $fields[$instance
        ->getName()] = $instance
        ->getLabel();
    }
  }
  if ($gc_field->type === 'text' && $gc_field->plain_text) {
    $fields['title'] = 'Title';
  }
  return $fields;
}