You are here

function label_help_form_alter in Label Help 8

Same name and namespace in other branches
  1. 7 label_help.module \label_help_form_alter()

Implements hook_form_alter().

File

./label_help.module, line 20
This is the file description for the Label Help module.

Code

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;
      }
    }
  }
}