public function LegacyWidget::flagErrors

Overrides Drupal\field\Plugin\Type\Widget\WidgetBase::flagErrors().

In D7, hook_field_widget_error() was supposed to call form_error() itself, whereas the new errorElement() method simply returns the element to flag. So we override the flagError() method to be more similar to the previous code in field_default_form_errors().

Overrides WidgetBase::flagErrors

File

drupal/core/modules/field/lib/Drupal/field/Plugin/field/widget/LegacyWidget.php, line 69
Definition of Drupal\field\Plugin\field\widget\LegacyWidget.

Class

LegacyWidget
Plugin implementation for legacy widgets.

Namespace

Drupal\field\Plugin\field\widget

Code

public function flagErrors(EntityInterface $entity, $langcode, array $items, array $form, array &$form_state) {
  $field_name = $this->field['field_name'];
  $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state);
  if (!empty($field_state['errors'])) {

    // Locate the correct element in the form.
    $element = drupal_array_get_nested_value($form_state['complete_form'], $field_state['array_parents']);

    // Only set errors if the element is accessible.
    if (!isset($element['#access']) || $element['#access']) {
      $definition = $this
        ->getDefinition();
      $is_multiple = $definition['multiple_values'];
      $function = $definition['module'] . '_field_widget_error';
      $function_exists = function_exists($function);
      foreach ($field_state['errors'] as $delta => $delta_errors) {

        // For multiple single-value widgets, pass errors by delta.
        // For a multiple-value widget, pass all errors to the main widget.
        $error_element = $is_multiple ? $element : $element[$delta];
        foreach ($delta_errors as $error) {
          if ($function_exists) {
            $function($error_element, $error, $form, $form_state);
          }
          else {

            // Make sure that errors are reported (even incorrectly flagged) if
            // the widget module fails to implement hook_field_widget_error().
            form_error($error_element, $error['message']);
          }
        }
      }

      // Reinitialize the errors list for the next submit.
      $field_state['errors'] = array();
      field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state);
    }
  }
}