You are here

public static function WebformSame::validateDestination in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformSame.php \Drupal\webform\Plugin\WebformElement\WebformSame::validateDestination()

Form API callback. Validate webform same as destination.

File

src/Plugin/WebformElement/WebformSame.php, line 98

Class

WebformSame
Provides a 'webform_same' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public static function validateDestination(array &$element, FormStateInterface $form_state) {

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = $form_state
    ->getFormObject()
    ->getWebform();

  // Get source and destination elements.
  $values = $form_state
    ->getValues();
  $source = $values['properties']['source'];
  $source_element = $webform
    ->getElement($source);
  $destination = $values['properties']['destination'];
  $destination_element = $webform
    ->getElement($destination);
  if ($destination === $source) {
    $form_state
      ->setError($element, t('The source and destination can not be the same element.'));
  }
  elseif ($destination_element['#type'] !== $source_element['#type']) {

    /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
    $element_manager = \Drupal::service('plugin.manager.webform.element');
    $element_plugin = $element_manager
      ->getElementInstance($element);
    $t_args = [
      '@type' => $element_plugin
        ->getPluginLabel(),
      '@source' => $source_element['#admin_title'],
      '@destination' => $destination_element['#admin_title'],
    ];
    $form_state
      ->setError($element, t('The destination element (@destination) must be the same element type (@type) as source element (@source).', $t_args));
  }
}