You are here

function better_field_descriptions_field_widget_form_alter in Better Field Descriptions 8

Implements hook_field_widget_form_alter().

Alter field forms as defined by the BFD configuration.

File

./better_field_descriptions.module, line 66
Module file for the Better field descriptions module.

Code

function better_field_descriptions_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {

  // Get better descriptions.
  $bfd = \Drupal::config('better_field_descriptions.settings')
    ->get('better_field_descriptions');

  // Get better descriptions settings.
  $bfd_settings = \Drupal::config('better_field_descriptions.settings')
    ->get('better_field_descriptions_settings');

  // Get the entity type, bundle, and field to check against bfd configuration.
  $field_name = $context['items']
    ->getName();
  $bundle = $context['items']
    ->getEntity()
    ->bundle();
  $entity_type = $context['items']
    ->getEntity()
    ->getEntityTypeId();

  // Verify if the better description for the field is enabled.
  if (isset($bfd_settings[$entity_type][$bundle][$field_name]) && !empty($bfd_settings[$entity_type][$bundle][$field_name])) {
    $data = $bfd[$entity_type][$bundle][$field_name];

    // Stop processing if this is just defaults.
    if (!empty($data['description']) || !empty($data['label']) || $data['position'] != 1) {

      // In many cases we want the first child of the widget instead.
      $use_child = FALSE;
      if (!isset($element['#title']) || isset($element['value'])) {
        $use_child = TRUE;
      }
      if ($use_child) {
        $child_keys = Element::children($element, TRUE);
        if (!empty($child_keys)) {
          $element =& $element[current($child_keys)];
        }
      }

      // The description as configured.
      $description = $data['description'];

      // Allow the original description to be used (only position will change).
      if (empty($description) && !empty($element['#description'])) {
        $description = $element['#description'];
        unset($element['#description']);
      }

      // Available positions.
      $positions = [
        0 => '#prefix',
        1 => '#suffix',
        2 => 'between',
      ];

      // Default label.
      $default_label = !empty($bfd['default_label']) ? FieldFilteredMarkup::create($bfd['default_label']) : '';

      // Apply required symbol if the fields is set as required.
      $required = isset($element['#required']) && $element['#required'] ? TRUE : FALSE;

      // Set position if configured or set default to #suffix.
      $position = isset($data['position']) ? $positions[$data['position']] : '#suffix';
      switch ($position) {
        case '#suffix':
        case '#prefix':
          $label = empty($data['label']) ? $default_label : $data['label'];
          if (isset($element['#type'])) {
            if ($element['#type'] == 'managed_file' && $position == '#suffix') {
              $position = '#field_suffix';
            }
            if ($element['#type'] == 'managed_file' && $position == '#prefix') {
              $position = '#field_prefix';
            }
          }
          break;
        case 'between':
          $label = empty($data['label']) && !empty($element['#title']) ? $element['#title'] : $data['label'];
          $element['#title_display'] = 'invisible';

          // In some elements, the title is duplicated and must also be hidden.
          if (isset($element['#title'])) {
            better_field_descriptions_hide_duplicate_titles($element, $element['#title']);
          }
          $position = '#prefix';
          if (isset($element['#type']) && $element['#type'] == 'managed_file' && $position == '#prefix') {
            $position = '#field_prefix';
          }
          break;
      }
      $output = [
        '#theme' => 'better_field_descriptions',
        '#label' => FieldFilteredMarkup::create($label),
        '#description' => FieldFilteredMarkup::create($description),
        '#required' => FieldFilteredMarkup::create($required),
      ];
      $output = \Drupal::service('renderer')
        ->render($output);

      // Add description to element, appending to other markup if present.
      if (!isset($element[$position])) {
        $element[$position] = '';
      }
      $element[$position] .= $output;
    }
  }
}