You are here

public function MappingSteps::doValidate 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::doValidate()

Do validation.

File

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

Class

MappingSteps
Class MappingSteps.

Namespace

Drupal\gathercontent_ui\Form\MappingEditSteps

Code

public function doValidate(array &$form, FormStateInterface $formState) {
  $form_definition_elements = [
    'return',
    'form_build_id',
    'form_token',
    'form_id',
    'op',
  ];
  $non_data_elements = array_merge($form_definition_elements, [
    'content_type',
    'entity_type',
    'id',
    'updated',
    'gathercontent_project',
    'gathercontent_template',
    'er_mapping_type',
    'submit',
    'close',
  ]);
  $mapping_data = [];
  foreach ($formState
    ->getValues() as $key => $value) {
    if (!in_array($key, $non_data_elements)) {
      $mapping_data[$key] = $value;
    }
  }

  // Check if is translatable.
  $entity_type = empty($this->mapping
    ->getMappedEntityType()) ? $formState
    ->getValue('entity_type') : $this->mapping
    ->getMappedEntityType();
  $content_type = empty($this->mapping
    ->getContentType()) ? $formState
    ->getValue('content_type') : $this->mapping
    ->getContentType();
  $translatable = \Drupal::moduleHandler()
    ->moduleExists('content_translation') && \Drupal::service('content_translation.manager')
    ->isEnabled($entity_type, $content_type);

  // Validate if each language is used only once
  // for translatable content types.
  $content_lang = [];
  $metatag_lang = [];
  if ($translatable) {
    foreach ($mapping_data as $groupId => $group) {
      $groupType = isset($group['type']) ? $group['type'] : 'content';
      if ($group['language'] != 'und') {
        if (!in_array($group['language'], ${$groupType . '_lang'})) {
          ${$groupType . '_lang'}[] = $group['language'];
        }
        else {
          $element = $groupId . '[language]';
          $formState
            ->setErrorByName($element, $this
            ->t('Each language can be used only once'));
        }
      }
    }
  }

  // Validate if each field is used only once.
  $content_fields = [];
  $metatag_fields = [];
  if ($translatable) {
    foreach ($content_lang as $lang) {
      $content_fields[$lang] = [];
    }
    foreach ($metatag_lang as $lang) {
      $metatag_fields[$lang] = [];
    }
    $content_fields[LanguageInterface::LANGCODE_NOT_SPECIFIED] = $metatag_fields[LanguageInterface::LANGCODE_NOT_SPECIFIED] = [];
  }
  foreach ($mapping_data as $groupId => $group) {
    $groupType = isset($group['type']) ? $group['type'] : 'content';
    if (isset($group['elements'])) {
      foreach ($group['elements'] as $k => $element) {
        if (empty($element)) {
          continue;
        }
        if ($translatable) {
          if ($groupType == 'content' && in_array($this->template['related']->structure->groups[$groupId]->fields[$k]->type, [
            'text',
            'guidelines',
          ]) || !in_array($element, ${$groupType . '_fields'}[$group['language']])) {
            ${$groupType . '_fields'}[$group['language']][] = $element;
          }
          else {
            if (!strpos($element, '||')) {
              $formState
                ->setErrorByName($groupId, $this
                ->t('A GatherContent field can only be mapped to a single Drupal field. So each field can only be mapped to once.'));
            }
          }
        }
        else {
          if ($groupType == 'content' && in_array($this->template['related']->structure->groups[$groupId]->fields[$k]->type, [
            'text',
            'guidelines',
          ]) || !in_array($element, ${$groupType . '_fields'})) {
            ${$groupType . '_fields'}[] = $element;
          }
          else {
            if (!strpos($element, '||')) {
              $formState
                ->setErrorByName($groupId, $this
                ->t('A GatherContent field can only be mapped to a single Drupal field. So each field can only be mapped to once.'));
            }
          }
        }
      }
    }
  }

  // Validate if at least one field in mapped.
  if (!$translatable && empty($content_fields) && empty($metatag_fields)) {
    $formState
      ->setErrorByName('form', $this
      ->t('You need to map at least one field to create mapping.'));
  }
  elseif ($translatable && count($content_fields) === 1 && empty($content_fields[LanguageInterface::LANGCODE_NOT_SPECIFIED]) && empty($metatag_fields[LanguageInterface::LANGCODE_NOT_SPECIFIED]) && count($metatag_fields) === 1) {
    $formState
      ->setErrorByName('form', $this
      ->t('You need to map at least one field to create mapping.'));
  }

  // Validate if title is mapped for translatable content.
  if ($translatable) {
    $titleField = $entity_type . '.' . $content_type . '.title';
    foreach ($content_fields as $k => $lang_fields) {
      if (!in_array($titleField, $lang_fields) && !in_array('title', $lang_fields) && $k !== LanguageInterface::LANGCODE_NOT_SPECIFIED) {
        $formState
          ->setErrorByName('form', $this
          ->t('You have to map Drupal Title field for translatable content.'));
      }
    }
  }
}