2b892e99dd6845521970cf7b106bd2f7b44fa7a7
[yaffs-website] / web / core / modules / inline_form_errors / src / FormErrorHandler.php
1 <?php
2
3 namespace Drupal\inline_form_errors;
4
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;
13 use Drupal\Core\Url;
14 use Drupal\Core\Utility\LinkGeneratorInterface;
15
16 /**
17  * Produces inline form errors.
18  */
19 class FormErrorHandler extends CoreFormErrorHandler {
20
21   use StringTranslationTrait;
22   use LinkGeneratorTrait;
23
24   /**
25    * The renderer service.
26    *
27    * @var \Drupal\Core\Render\RendererInterface
28    */
29   protected $renderer;
30
31   /**
32    * Constructs a new FormErrorHandler.
33    *
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.
40    */
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;
45   }
46
47   /**
48    * Loops through and displays all form errors.
49    *
50    * @param array $form
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.
54    */
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);
60       return;
61     }
62
63     $error_links = [];
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);
69
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']);
77
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]);
82       }
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]);
86       }
87     }
88
89     // Set normal error messages for all remaining errors.
90     foreach ($errors as $error) {
91       $this->drupalSetMessage($error, 'error');
92     }
93
94     if (!empty($error_links)) {
95       $render_array = [
96         [
97          '#markup' => $this->formatPlural(count($error_links), '1 error has been found: ', '@count errors have been found: '),
98         ],
99         [
100           '#theme' => 'item_list',
101           '#items' => $error_links,
102           '#context' => ['list_style' => 'comma-list'],
103         ],
104       ];
105       $message = $this->renderer->renderPlain($render_array);
106       $this->drupalSetMessage($message, 'error');
107     }
108   }
109
110 }