You are here

public function FieldInheritanceForm::validateForm in Field Inheritance 8

Same name and namespace in other branches
  1. 2.0.x src/Form/FieldInheritanceForm.php \Drupal\field_inheritance\Form\FieldInheritanceForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/FieldInheritanceForm.php, line 429

Class

FieldInheritanceForm
Provides a form for managing field inheritance entities.

Namespace

Drupal\field_inheritance\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $values = $form_state
    ->getValues();
  if (!empty($values['source_entity_type']) && !empty($values['destination_entity_type']) && !empty($values['source_entity_bundle']) && !empty($values['destination_entity_bundle'])) {
    if (!empty($values['source_field']) && !empty($values['destination_field'])) {
      $source_definitions = $this->entityFieldManager
        ->getFieldDefinitions($values['source_entity_type'], $values['source_entity_bundle']);
      $destination_definitions = $this->entityFieldManager
        ->getFieldDefinitions($values['destination_entity_type'], $values['destination_entity_bundle']);
      if ($source_definitions[$values['source_field']]
        ->getType() !== $destination_definitions[$values['destination_field']]
        ->getType()) {
        $message = $this
          ->t('Source and destination field definition types must be the same to inherit data. Source - @source_name type: @source_type. Destination - @destination_name type: @destination_type', [
          '@source_name' => $values['source_field'],
          '@source_type' => $source_definitions[$values['source_field']]
            ->getType(),
          '@destination_name' => $values['destination_field'],
          '@destination_type' => $destination_definitions[$values['destination_field']]
            ->getType(),
        ]);
        $form_state
          ->setErrorByName('source_field', $message);
        $form_state
          ->setErrorByName('destination_field', $message);
      }
      $plugin_definition = $this->fieldInheritance
        ->getDefinition($values['plugin']);
      $field_types = $plugin_definition['types'];
      if (!in_array('any', $field_types) && !in_array($source_definitions[$values['source_field']]
        ->getType(), $field_types)) {
        $message = $this
          ->t('The selected plugin @plugin does not support @source_type fields. The supported field types are: @field_types', [
          '@plugin' => $values['plugin'],
          '@source_type' => $source_definitions[$values['source_field']]
            ->getType(),
          '@field_types' => implode(',', $field_types),
        ]);
        $form_state
          ->setErrorByName('source_field', $message);
        $form_state
          ->setErrorByName('plugin', $message);
      }
      if ($values['source_entity_type'] == $values['destination_entity_type'] && $values['source_entity_bundle'] == $values['destination_entity_bundle']) {
        $message = $this
          ->t('You cannot inherit if the source and destination entities and bundles are the same.');
        $form_state
          ->setErrorByName('source_entity_bundle', $message);
        $form_state
          ->setErrorByName('destination_entity_bundle', $message);
      }
    }
  }
}