X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Finline_form_errors%2Fsrc%2FFormErrorHandler.php;fp=web%2Fcore%2Fmodules%2Finline_form_errors%2Fsrc%2FFormErrorHandler.php;h=2b892e99dd6845521970cf7b106bd2f7b44fa7a7;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/inline_form_errors/src/FormErrorHandler.php b/web/core/modules/inline_form_errors/src/FormErrorHandler.php new file mode 100644 index 000000000..2b892e99d --- /dev/null +++ b/web/core/modules/inline_form_errors/src/FormErrorHandler.php @@ -0,0 +1,110 @@ +stringTranslation = $string_translation; + $this->linkGenerator = $link_generator; + $this->renderer = $renderer; + } + + /** + * Loops through and displays all form errors. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + protected function displayErrorMessages(array $form, FormStateInterface $form_state) { + // Use the original error display for Quick Edit forms, because in this case + // the errors are already near the form element. + if ($form['#form_id'] === 'quickedit_field_form') { + 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[] = $this->l($title, Url::fromRoute('', [], ['fragment' => $form_element['#id'], 'external' => TRUE])); + unset($errors[$name]); + } + } + + // Set normal error messages for all remaining errors. + foreach ($errors as $error) { + $this->drupalSetMessage($error, '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->renderPlain($render_array); + $this->drupalSetMessage($message, 'error'); + } + } + +}