label_help.module in Label Help 8
Same filename and directory in other branches
This is the file description for the Label Help module.
In normal rendering of textarea form fields that are text-format-enabled, the field's help text gets placed below the text format selector and tips. This means that website users are unlikely to notice and read the help text. This module creates help text to be placed directly below the field's label.
File
label_help.moduleView source
<?php
use Drupal\Core\Render\Element;
use Drupal\Core\Form\FormStateInterface;
/**
* @file
* This is the file description for the Label Help module.
*
* In normal rendering of textarea form fields that are text-format-enabled,
* the field's help text gets placed below the text format selector and tips.
* This means that website users are unlikely to notice and read the help text.
* This module creates help text to be placed directly below the field's
* label.
*/
/**
* Implements hook_form_alter().
*/
function label_help_form_alter(&$form, &$form_state, $form_id) {
$children = array_intersect_key($form, array_flip(Element::children($form)));
foreach ($children as $key => $item) {
$form_object = $form_state
->getFormObject();
if (!method_exists($form_object, 'getEntity')) {
continue;
}
$form_entity = $form_object
->getEntity();
if (!method_exists($form_entity, 'getFieldDefinition')) {
continue;
}
$method = new ReflectionMethod($form_entity, 'getFieldDefinition');
if (!$method
->isPublic()) {
continue;
}
$form_entity_get_field_definition_method = new ReflectionMethod($form_entity, 'getFieldDefinition');
$field = $form_object
->getEntity()
->getFieldDefinition($key);
$label_help = NULL;
if (method_exists($field, 'getThirdPartySetting')) {
$label_help = $field
->getThirdPartySetting('label_help', 'label_help_description');
}
if (!is_null($label_help)) {
$theme_option = array(
'description at top' => $label_help,
);
// Put comments above the label for field forms of type 'container'
// that are specifically configured.
if (isset($item['#type']) && $item['#type'] == 'container') {
// For reasons best known to the lunatics who designed the Forms API,
// $form[$key][$item['#language']][0]['#theme_options'] has to be set to get
// this working for textarea fields, and
// form[$key][$item['#language']][0]['value']['#theme_options'] has to be set
// to get this working for one-line text fields, and
// form[$key][$item['#language']][0]['default']['#theme_options'] has to be set
// to get this working for some other fields.
$label_help_markup = t('<div class="description label-description">@label_help</div>', [
'@label_help' => $label_help,
]);
if (isset($form[$key]['widget'][0]['value'])) {
$form[$key]['widget'][0]['value']['#label_suffix'] = $label_help_markup;
}
elseif (isset($form[$key]['widget'][0]['#title'])) {
$form[$key]['widget'][0]['#label_suffix'] = $label_help_markup;
}
elseif (isset($form[$key]['widget']['#title'])) {
$form[$key]['widget']['#label_suffix'] = $label_help_markup;
}
elseif (isset($form[$key]['widget']['target_id']['#title'])) {
$form[$key]['widget']['target_id']['#label_suffix'] = $label_help_markup;
}
// One more for cshs module.
// elseif (isset($form[$key][$item['#language']][0]['tid'])) {
// $form[$key][$item['#language']][0]['tid']['#theme_options'] = $theme_option;
// }
}
else {
$form[$key]['widget']['#label_suffix'] = $label_help_markup;
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function label_help_form_field_config_edit_form_alter(&$form, &$form_state, $form_id) {
$fieldConfig = $form_state
->getFormObject()
->getEntity();
// Add settings for file upload widgets.
$form['settings']['label_help_description'] = array(
'#type' => 'textarea',
'#rows' => 2,
'#title' => t('Label help message'),
'#default_value' => $fieldConfig
->getThirdPartySetting('label_help', 'label_help_description'),
'#description' => t('Help text to insert below the label and above the input form element.'),
);
$form['#entity_builders'][] = 'label_help_form_field_config_edit_form_builder';
}
/**
* Entity builder for the menu configuration entity.
*/
function label_help_form_field_config_edit_form_builder($entity_type, $field, &$form, FormStateInterface $form_state) {
if ($form_state
->getValue([
'settings',
'label_help_description',
])) {
$field
->setThirdPartySetting('label_help', 'label_help_description', $form_state
->getValue([
'settings',
'label_help_description',
]));
return;
}
$field
->unsetThirdPartySetting('label_help', 'label_help_description');
}
/**
* Implements hook_preprocess_HOOK().
*/
function label_help_preprocess_form_element(&$variables) {
if (!isset($variables['element']['#name'])) {
return;
}
// setting a label suffix/prefix
$element =& $variables['element'];
if (!empty($variables['label'])) {
if (!empty($element['#label_prefix'])) {
$variables['label']['#prefix'] = $element['#label_prefix'];
}
if (!empty($element['#label_suffix'])) {
$variables['label']['#suffix'] = $element['#label_suffix'];
}
}
}
Functions
Name![]() |
Description |
---|---|
label_help_form_alter | Implements hook_form_alter(). |
label_help_form_field_config_edit_form_alter | Implements hook_form_FORM_ID_alter(). |
label_help_form_field_config_edit_form_builder | Entity builder for the menu configuration entity. |
label_help_preprocess_form_element | Implements hook_preprocess_HOOK(). |