function FormErrorHandler::displayErrorMessages

Same name in this branch
  1. 11.x core/lib/Drupal/Core/Form/FormErrorHandler.php \Drupal\Core\Form\FormErrorHandler::displayErrorMessages()
Same name and namespace in other branches
  1. 9 core/modules/inline_form_errors/src/FormErrorHandler.php \Drupal\inline_form_errors\FormErrorHandler::displayErrorMessages()
  2. 9 core/lib/Drupal/Core/Form/FormErrorHandler.php \Drupal\Core\Form\FormErrorHandler::displayErrorMessages()
  3. 8.9.x core/modules/inline_form_errors/src/FormErrorHandler.php \Drupal\inline_form_errors\FormErrorHandler::displayErrorMessages()
  4. 8.9.x core/lib/Drupal/Core/Form/FormErrorHandler.php \Drupal\Core\Form\FormErrorHandler::displayErrorMessages()
  5. 10 core/modules/inline_form_errors/src/FormErrorHandler.php \Drupal\inline_form_errors\FormErrorHandler::displayErrorMessages()
  6. 10 core/lib/Drupal/Core/Form/FormErrorHandler.php \Drupal\Core\Form\FormErrorHandler::displayErrorMessages()

Loops through and displays all form errors.

To disable inline form errors for an entire form set the #disable_inline_form_errors property to TRUE on the top level of the $form array:

$form['#disable_inline_form_errors'] = TRUE;

This should only be done when another appropriate accessibility strategy is in place.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormErrorHandler::displayErrorMessages

File

core/modules/inline_form_errors/src/FormErrorHandler.php, line 70

Class

FormErrorHandler
Produces inline form errors.

Namespace

Drupal\inline_form_errors

Code

protected function displayErrorMessages(array $form, FormStateInterface $form_state) {
    // Skip generating inline form errors when opted out.
    if (!empty($form['#disable_inline_form_errors'])) {
        parent::displayErrorMessages($form, $form_state);
        return;
    }
    $error_links = [];
    $errors = $form_state->getErrors();
    // Loop through all form errors and check if we need to display a link.
    foreach ($errors as $name => $error) {
        $form_element = FormElementHelper::getElementByName($name, $form);
        $title = FormElementHelper::getElementTitle($form_element);
        // Only show links to erroneous elements that are visible.
        $is_visible_element = Element::isVisibleElement($form_element);
        // Only show links for elements that have a title themselves or have
        // children with a title.
        $has_title = !empty($title);
        // Only show links for elements with an ID.
        $has_id = !empty($form_element['#id']);
        // Do not show links to elements with suppressed messages. Most often
        // their parent element is used for inline errors.
        if (!empty($form_element['#error_no_message'])) {
            unset($errors[$name]);
        }
        elseif ($is_visible_element && $has_title && $has_id) {
            $error_links[] = Link::fromTextAndUrl($title, Url::fromRoute('<none>', [], [
                'fragment' => $form_element['#id'],
                'external' => TRUE,
            ]))->toRenderable();
            unset($errors[$name]);
        }
    }
    // Set normal error messages for all remaining errors.
    foreach ($errors as $error) {
        $this->messenger
            ->addError($error);
    }
    if (!empty($error_links)) {
        $render_array = [
            [
                '#markup' => $this->formatPlural(count($error_links), '1 error has been found: ', '@count errors have been found: '),
            ],
            [
                '#theme' => 'item_list',
                '#items' => $error_links,
                '#context' => [
                    'list_style' => 'comma-list',
                ],
            ],
        ];
        $message = $this->renderer
            ->renderInIsolation($render_array);
        $this->messenger
            ->addError($message);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.