You are here

class FormErrorHandler in Zircon Profile 8

Same name in this branch
  1. 8 core/modules/inline_form_errors/src/FormErrorHandler.php \Drupal\inline_form_errors\FormErrorHandler
  2. 8 core/lib/Drupal/Core/Form/FormErrorHandler.php \Drupal\Core\Form\FormErrorHandler
Same name and namespace in other branches
  1. 8.0 core/modules/inline_form_errors/src/FormErrorHandler.php \Drupal\inline_form_errors\FormErrorHandler

Produces inline form errors.

Hierarchy

Expanded class hierarchy of FormErrorHandler

1 file declares its use of FormErrorHandler
FormErrorHandlerTest.php in core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php
Contains \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest.

File

core/modules/inline_form_errors/src/FormErrorHandler.php, line 24
Contains \Drupal\inline_form_errors\FormErrorHandler.

Namespace

Drupal\inline_form_errors
View source
class FormErrorHandler extends CoreFormErrorHandler {
  use StringTranslationTrait;
  use LinkGeneratorTrait;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new FormErrorHandler.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
   *   The link generation service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  public function __construct(TranslationInterface $string_translation, LinkGeneratorInterface $link_generator, RendererInterface $renderer) {
    $this->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) {
    $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('<none>', [], [
          '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');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormErrorHandler::$renderer protected property The renderer service.
FormErrorHandler::displayErrorMessages protected function Loops through and displays all form errors. Overrides FormErrorHandler::displayErrorMessages
FormErrorHandler::drupalSetMessage protected function Wraps drupal_set_message().
FormErrorHandler::handleFormErrors public function Handles form errors after form validation. Overrides FormErrorHandlerInterface::handleFormErrors
FormErrorHandler::setElementErrorsFromFormState protected function Stores the errors of each element directly on the element.
FormErrorHandler::__construct public function Constructs a new FormErrorHandler.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator protected function Returns the link generator.
LinkGeneratorTrait::l protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator public function Sets the link generator service.
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.