You are here

public static function LanguageAutocompleteWidget::validateElement in Custom Language field 8

Form element validate handler for language autocomplete element.

Parameters

mixed $element: Element.

\Drupal\Core\Form\FormStateInterface $form_state: Form state interface.

1 method overrides LanguageAutocompleteWidget::validateElement()
LanguageAutocompleteTagsWidget::validateElement in src/Plugin/Field/FieldWidget/LanguageAutocompleteTagsWidget.php
Form element validate handler for language autocomplete tags element.

File

src/Plugin/Field/FieldWidget/LanguageAutocompleteWidget.php, line 82

Class

LanguageAutocompleteWidget
Plugin implementation of the 'languagefield_autocomplete' widget.

Namespace

Drupal\languagefield\Plugin\Field\FieldWidget

Code

public static function validateElement($element, FormStateInterface $form_state) {
  if (!($input = $element['#value'])) {
    return;
  }
  $languages = $element['#languagefield_options'];
  $langcode = array_search($input, $languages);
  if (!empty($langcode)) {
    $form_state
      ->setValueForElement($element, $langcode);
  }
  else {
    $form_state
      ->setError($element, t('An unexpected language is entered.'));
  }
}