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.
51 * An associative array containing the structure of the form.
52 * @param \Drupal\Core\Form\FormStateInterface $form_state
53 * The current state of the form.
55 protected function displayErrorMessages(array $form, FormStateInterface $form_state) {
56 // Use the original error display for Quick Edit forms, because in this case
57 // the errors are already near the form element.
58 if ($form['#form_id'] === 'quickedit_field_form') {
59 parent::displayErrorMessages($form, $form_state);
64 $errors = $form_state->getErrors();
65 // Loop through all form errors and check if we need to display a link.
66 foreach ($errors as $name => $error) {
67 $form_element = FormElementHelper::getElementByName($name, $form);
68 $title = FormElementHelper::getElementTitle($form_element);
70 // Only show links to erroneous elements that are visible.
71 $is_visible_element = Element::isVisibleElement($form_element);
72 // Only show links for elements that have a title themselves or have
73 // children with a title.
74 $has_title = !empty($title);
75 // Only show links for elements with an ID.
76 $has_id = !empty($form_element['#id']);
78 // Do not show links to elements with suppressed messages. Most often
79 // their parent element is used for inline errors.
80 if (!empty($form_element['#error_no_message'])) {
81 unset($errors[$name]);
83 elseif ($is_visible_element && $has_title && $has_id) {
84 $error_links[] = $this->l($title, Url::fromRoute('<none>', [], ['fragment' => $form_element['#id'], 'external' => TRUE]));
85 unset($errors[$name]);
89 // Set normal error messages for all remaining errors.
90 foreach ($errors as $error) {
91 $this->drupalSetMessage($error, 'error');
94 if (!empty($error_links)) {
97 '#markup' => $this->formatPlural(count($error_links), '1 error has been found: ', '@count errors have been found: '),
100 '#theme' => 'item_list',
101 '#items' => $error_links,
102 '#context' => ['list_style' => 'comma-list'],
105 $message = $this->renderer->renderPlain($render_array);
106 $this->drupalSetMessage($message, 'error');