You are here

public function WebformTokenManager::elementValidate in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformTokenManager.php \Drupal\webform\WebformTokenManager::elementValidate()

Validate form that should have tokens in it.

Parameters

array $form: A form.

array $token_types: An array containing token types that should be validated.

Overrides WebformTokenManagerInterface::elementValidate

See also

token_element_validate()

File

src/WebformTokenManager.php, line 293

Class

WebformTokenManager
Defines a class to manage token replacement.

Namespace

Drupal\webform

Code

public function elementValidate(array &$form, array $token_types = [
  'webform',
  'webform_submission',
  'webform_handler',
]) {
  if (!function_exists('token_element_validate')) {
    return;
  }

  // Always add system tokens.
  // @see system_token_info()
  $token_types = array_merge($token_types, [
    'site',
    'date',
  ]);
  $text_element_types = [
    'email' => 'email',
    'textfield' => 'textfield',
    'textarea' => 'textarea',
    'url' => 'url',
    'webform_codemirror' => 'webform_codemirror',
    'webform_email_multiple' => 'webform_email_multiple',
    'webform_html_editor' => 'webform_html_editor',
    'webform_checkboxes_other' => 'webform_checkboxes_other',
    'webform_select_other' => 'webform_select_other',
    'webform_radios_other' => 'webform_radios_other',
  ];

  // If $form render array is an element then see if we should add
  // validation callback.
  if (isset($form['#type']) && isset($text_element_types[$form['#type']])) {
    $form['#element_validate'][] = [
      get_called_class(),
      'validateElement',
    ];
    $form['#token_types'] = $token_types;
  }
  $elements =& WebformFormHelper::flattenElements($form);
  foreach ($elements as &$element) {
    if (!isset($element['#type']) || !isset($text_element_types[$element['#type']])) {
      continue;
    }
    $element['#element_validate'][] = [
      get_called_class(),
      'validateElement',
    ];
    $element['#token_types'] = $token_types;
  }
}