3 namespace Drupal\inline_form_errors;
5 use Drupal\Core\Form\FormElementHelper;
6 use Drupal\Core\Form\FormErrorHandler as CoreFormErrorHandler;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Render\Element;
9 use Drupal\Core\Routing\LinkGeneratorTrait;
10 use Drupal\Core\Render\RendererInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use Drupal\Core\StringTranslation\TranslationInterface;
14 use Drupal\Core\Utility\LinkGeneratorInterface;
17 * Produces inline form errors.
19 class FormErrorHandler extends CoreFormErrorHandler {
21 use StringTranslationTrait;
22 use LinkGeneratorTrait;
25 * The renderer service.
27 * @var \Drupal\Core\Render\RendererInterface
32 * Constructs a new FormErrorHandler.
34 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
35 * The string translation service.
36 * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
37 * The link generation service.
38 * @param \Drupal\Core\Render\RendererInterface $renderer
39 * The renderer service.
41 public function __construct(TranslationInterface $string_translation, LinkGeneratorInterface $link_generator, RendererInterface $renderer) {
42 $this->stringTranslation = $string_translation;
43 $this->linkGenerator = $link_generator;
44 $this->renderer = $renderer;
48 * Loops through and displays all form errors.
50 * To disable inline form errors for an entire form set the
51 * #disable_inline_form_errors property to TRUE on the top level of the $form
54 * $form['#disable_inline_form_errors'] = TRUE;
56 * This should only be done when another appropriate accessibility strategy is
60 * An associative array containing the structure of the form.
61 * @param \Drupal\Core\Form\FormStateInterface $form_state
62 * The current state of the form.
64 protected function displayErrorMessages(array $form, FormStateInterface $form_state) {
65 // Skip generating inline form errors when opted out.
66 if (!empty($form['#disable_inline_form_errors'])) {
67 parent::displayErrorMessages($form, $form_state);
72 $errors = $form_state->getErrors();
73 // Loop through all form errors and check if we need to display a link.
74 foreach ($errors as $name => $error) {
75 $form_element = FormElementHelper::getElementByName($name, $form);
76 $title = FormElementHelper::getElementTitle($form_element);
78 // Only show links to erroneous elements that are visible.
79 $is_visible_element = Element::isVisibleElement($form_element);
80 // Only show links for elements that have a title themselves or have
81 // children with a title.
82 $has_title = !empty($title);
83 // Only show links for elements with an ID.
84 $has_id = !empty($form_element['#id']);
86 // Do not show links to elements with suppressed messages. Most often
87 // their parent element is used for inline errors.
88 if (!empty($form_element['#error_no_message'])) {
89 unset($errors[$name]);
91 elseif ($is_visible_element && $has_title && $has_id) {
92 $error_links[] = $this->l($title, Url::fromRoute('<none>', [], ['fragment' => $form_element['#id'], 'external' => TRUE]));
93 unset($errors[$name]);
97 // Set normal error messages for all remaining errors.
98 foreach ($errors as $error) {
99 $this->drupalSetMessage($error, 'error');
102 if (!empty($error_links)) {
105 '#markup' => $this->formatPlural(count($error_links), '1 error has been found: ', '@count errors have been found: '),
108 '#theme' => 'item_list',
109 '#items' => $error_links,
110 '#context' => ['list_style' => 'comma-list'],
113 $message = $this->renderer->renderPlain($render_array);
114 $this->drupalSetMessage($message, 'error');