You are here

function domain_source_form_validate in Domain Access 8

Validate form submissions.

1 string reference to 'domain_source_form_validate'
domain_source_form_alter in domain_source/domain_source.module
Implements hook_form_alter().

File

domain_source/domain_source.module, line 152
Domain-based path rewrites for content.

Code

function domain_source_form_validate($element, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();

  // This is only run if Domain Access is present.
  if (isset($values[DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD]) && \Drupal::moduleHandler()
    ->moduleExists('domain_access') && isset($values[DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD])) {
    $access_values = $values[DomainAccessManagerInterface::DOMAIN_ACCESS_FIELD];
    $source_value = current($values[DomainSourceElementManagerInterface::DOMAIN_SOURCE_FIELD]);
  }

  // If no value is selected, that's acceptable. Else run through a check.
  // Note that the _none selection returns as [FALSE].
  $source_valid = FALSE;
  if (empty($source_value)) {
    $source_valid = TRUE;
  }
  else {
    foreach ($access_values as $value) {

      // Core is inconsistent depending on the field order.
      // See https://www.drupal.org/project/domain/issues/2945771#comment-12493199
      if (is_array($value) && $value == $source_value) {
        $source_valid = TRUE;
      }
      elseif (is_string($value) && !empty($source_value['target_id']) && $value == $source_value['target_id']) {
        $source_valid = TRUE;
      }
    }
  }
  if (!$source_valid) {
    $form_state
      ->setError($element, t('The source domain must be selected as a publishing option.'));
  }
}