Same filename and directory in other branches
  1. 8.9.x core/modules/inline_form_errors/inline_form_errors.module
  2. 9 core/modules/inline_form_errors/inline_form_errors.module

Enables inline form errors.

File

core/modules/inline_form_errors/inline_form_errors.module
View source
<?php

/**
 * @file
 * Enables inline form errors.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\inline_form_errors\RenderElementHelper;

/**
 * Implements hook_help().
 */
function inline_form_errors_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.inline_form_errors':
      $output = '';
      $output .= '<h2>' . t('About') . '</h2>';
      $output .= '<p>' . t('The Inline Form Errors module makes it easier for users to identify which errors need to be resolved by providing a summary of all errors and by placing the individual error messages next to the form elements themselves. For more information, see the <a href=":inline_form_error">online documentation for the Inline Form Errors module</a>.', [
        ':inline_form_error' => 'https://www.drupal.org/docs/8/core/modules/inline-form-errors',
      ]) . '</p>';
      $output .= '<h2>' . t('Uses') . '</h2>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Displaying error messages') . '</dt>';
      $output .= '<dd>' . t('When a form is not filled in correctly (for example, if a required field is left blank), a warning message is displayed at the top of the form. This message links to the affected form elements, and individual error messages are displayed next to each form element.') . '</dd>';
      $output .= '<dt>' . t('Displaying error messages in browsers with HTML5 form validation') . '</dt>';
      $output .= '<dd>' . t('In browsers that support HTML5 form validation, users will first see the error messages generated by their browser. In this case, the inline form error messages are only displayed after the HTML5 validation errors have been resolved.') . '</dd>';
      return $output;
  }
}

/**
 * Implements hook_preprocess_HOOK() for form element templates.
 */
function inline_form_errors_preprocess_form_element(&$variables) {
  _inline_form_errors_set_errors($variables);
}

/**
 * Implements hook_preprocess_HOOK() for details element templates.
 */
function inline_form_errors_preprocess_details(&$variables) {
  _inline_form_errors_set_errors($variables);
}

/**
 * Implements hook_preprocess_HOOK() for fieldset element templates.
 */
function inline_form_errors_preprocess_fieldset(&$variables) {
  _inline_form_errors_set_errors($variables);
}

/**
 * Implements hook_preprocess_HOOK() for datetime form wrapper templates.
 */
function inline_form_errors_preprocess_datetime_wrapper(&$variables) {
  _inline_form_errors_set_errors($variables);
}

/**
 * Implements hook_element_info_alter().
 */
function inline_form_errors_element_info_alter(array &$info) {
  \Drupal::classResolver(RenderElementHelper::class)
    ->alterElementInfo($info);
}

/**
 * Populates form errors in the template.
 */
function _inline_form_errors_set_errors(&$variables) {
  $element = $variables['element'];
  if (!empty($element['#errors']) && empty($element['#error_no_message'])) {
    $variables['errors'] = $element['#errors'];
  }
}

Functions

Namesort descending Description
inline_form_errors_element_info_alter Implements hook_element_info_alter().
inline_form_errors_help Implements hook_help().
inline_form_errors_preprocess_datetime_wrapper Implements hook_preprocess_HOOK() for datetime form wrapper templates.
inline_form_errors_preprocess_details Implements hook_preprocess_HOOK() for details element templates.
inline_form_errors_preprocess_fieldset Implements hook_preprocess_HOOK() for fieldset element templates.
inline_form_errors_preprocess_form_element Implements hook_preprocess_HOOK() for form element templates.
_inline_form_errors_set_errors Populates form errors in the template.