You are here

public function FlowForm::validateForm in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::validateForm()
  2. 2.0.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::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/FlowForm.php, line 517

Class

FlowForm
Form handler for the Flow add and edit forms.

Namespace

Drupal\cms_content_sync\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $entity_types = [];
  $entity_types_with_fields = [];
  $current_values = $this
    ->getCurrentValues($form_state);
  $used_pools = [];
  foreach ($current_values['sync_entities'] as $key => &$config) {
    if ('#' == $key[0]) {
      continue;
    }
    $count = substr_count($key, '-');
    if (0 == $count) {
      continue;
    }
    $values = $config;
    if (empty($values['handler']) || Flow::HANDLER_IGNORE == $values['handler']) {
      continue;
    }
    if (1 == $count) {
      list($entity_type, $bundle) = explode('-', $key);
      $entity_types[] = $entity_type . '-' . $bundle;
      $handler = $this->entityPluginManager
        ->createInstance($values['handler'], [
        'entity_type_name' => $entity_type,
        'bundle_name' => $bundle,
        'settings' => $values,
        'sync' => null,
      ]);
      if (!empty($values['export']) && PushIntent::PUSH_DISABLED !== $values['export']) {
        foreach ($values['export_pools'] as $name => $behavior) {
          if (Pool::POOL_USAGE_FORBID !== $behavior) {
            $used_pools[$name] = true;
          }
        }
      }
      if (!empty($values['import']) && PullIntent::PULL_DISABLED !== $values['import']) {
        foreach ($values['import_pools'] as $name => $behavior) {
          if (Pool::POOL_USAGE_FORBID !== $behavior) {
            $used_pools[$name] = true;
          }
        }
      }
    }
    else {
      list($entity_type, $bundle, $field) = explode('-', $key);
      $entity_types_with_fields[] = $entity_type . '-' . $bundle;

      /**
       * @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields
       */
      $field_definition = $this->entityFieldManager
        ->getFieldDefinitions($entity_type, $bundle)[$field];
      $handler = $this->fieldPluginManager
        ->createInstance($values['handler'], [
        'entity_type_name' => $entity_type,
        'bundle_name' => $bundle,
        'field_name' => $field,
        'field_definition' => $field_definition,
        'settings' => $values,
        'sync' => null,
      ]);
    }
    $handler
      ->validateHandlerSettings($form, $form_state, $key, $current_values);
  }

  // Validate that only one Sync Core is used per Flow.
  $pools = Pool::getAll();
  $sync_core_url = null;
  foreach ($used_pools as $name => $used) {
    if (null === $sync_core_url) {
      $sync_core_url = $pools[$name]
        ->getSyncCoreUrl();
    }
    elseif ($sync_core_url !== $pools[$name]
      ->getSyncCoreUrl()) {
      $form_state
        ->setErrorByName('', $this
        ->t('You can only use Pools from one Sync Core per Flow.'));
      break;
    }
  }
  if (Flow::TYPE_PUSH !== $this
    ->getCurrentFormType()) {
    $error = $this
      ->validateSyncCoreAccessToSite($sync_core_url);
    if ($error) {
      if ($current_values['status']) {
        $form_state
          ->setErrorByName('name', $error);
        \Drupal::messenger()
          ->addWarning($this
          ->t('To save this Flow anyway you can set it to inactive and try again.'));
      }
      else {
        \Drupal::messenger()
          ->addWarning($error);
      }
    }
  }
  return parent::validateForm($form, $form_state);
}