FormErrorHandler.php in Zircon Profile 8
File
core/modules/inline_form_errors/src/FormErrorHandler.php
View source
<?php
namespace Drupal\inline_form_errors;
use Drupal\Core\Form\FormElementHelper;
use Drupal\Core\Form\FormErrorHandler as CoreFormErrorHandler;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
class FormErrorHandler extends CoreFormErrorHandler {
use StringTranslationTrait;
use LinkGeneratorTrait;
protected $renderer;
public function __construct(TranslationInterface $string_translation, LinkGeneratorInterface $link_generator, RendererInterface $renderer) {
$this->stringTranslation = $string_translation;
$this->linkGenerator = $link_generator;
$this->renderer = $renderer;
}
protected function displayErrorMessages(array $form, FormStateInterface $form_state) {
$error_links = [];
$errors = $form_state
->getErrors();
foreach ($errors as $name => $error) {
$form_element = FormElementHelper::getElementByName($name, $form);
$title = FormElementHelper::getElementTitle($form_element);
$is_visible_element = Element::isVisibleElement($form_element);
$has_title = !empty($title);
$has_id = !empty($form_element['#id']);
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]);
}
}
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');
}
}
}