You are here

function field_label_preprocess_field in Field Label 8

Implements hook_preprocess_HOOK() for field templates.

File

./field_label.module, line 201
Provides custom settings to format field labels.

Code

function field_label_preprocess_field(&$variables) {

  // Get the module config.
  $config = \Drupal::config('field_label.settings');
  if (isset($variables['element']['#third_party_settings']['field_label'])) {
    $settings = $variables['element']['#third_party_settings']['field_label'];

    // Update the label.
    if ($config
      ->get('label_value_enabled') && !empty($settings['label_value'])) {
      $variables['label'] = $settings['label_value'];
    }

    // Create the class attribute array if it doesn't already exisit.
    if (!isset($variables['title_attributes']['class'])) {
      $variables['title_attributes']['class'] = [];
    }

    // Add any free-form classes.
    if ($config
      ->get('label_class_enabled') && !empty($settings['label_class'])) {
      $classes = explode(' ', $settings['label_class']);
      foreach ($classes as $class) {
        $variables['title_attributes']['class'][] = Html::cleanCssIdentifier($class);
      }
    }

    // Add class selected from class list.
    if ($config
      ->get('label_class_select_enabled') && !empty($settings['label_class_select'])) {
      if (_field_label_is_valid_style($settings['label_class_select'])) {
        $classes = explode('.', $settings['label_class_select']);
        foreach ($classes as $class) {
          $variables['title_attributes']['class'][] = Html::cleanCssIdentifier($class);
        }
      }
    }

    // Add the wrapper tag if enabled and set.
    if ($config
      ->get('label_tag_enabled') && !empty($settings['label_tag'])) {
      $variables['label_tag'] = $settings['label_tag'];
    }
  }
}