Skip to content
field_display_label.module 1.67 KiB
Newer Older
Kevin Miller's avatar
Kevin Miller committed
<?php

/**
*  Implementation of hook_form_FORM_ID_alter
*/
function field_display_label_form_field_ui_field_edit_form_alter(&$form, $form_state, $form_id) {
  if (!isset($form['instance'])) {
Kevin Miller's avatar
Kevin Miller committed
    return;
  }
Kevin Miller's avatar
Kevin Miller committed
  $form['instance']['display_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Display label'),
    '#description' => t('A separate label for viewing this field. Leave blank to use the default field label.'),
Kevin Miller's avatar
Kevin Miller committed
    '#default_value' => isset($form['#instance']['display_label']) ? $form['#instance']['display_label'] : '',
Kevin Miller's avatar
Kevin Miller committed
    '#weight' => $form['instance']['label']['#weight'] + 1,
  );
}

/**
*  Implementation of hook_field_display_alter
*/
function field_display_label_theme_registry_alter(&$theme_registry) {
  $theme_registry['field']['preprocess functions'][] = 'field_display_label_preprocess_field';
}

/**
*  Preprocess function to replace the regular label with the display label
*/
function field_display_label_preprocess_field(&$variables) {
  $field = field_info_instance($variables['element']['#entity_type'], $variables['element']['#field_name'], $variables['element']['#bundle']);
Kevin Miller's avatar
Kevin Miller committed
  if (isset($field['display_label']) && strlen(trim($field['display_label'])) > 0) {
    if (module_exists('i18n_field')) {
      $variables['label'] = check_plain(i18n_field_translate_property($field, 'display_label'));
    }
    else {
      $variables['label'] = check_plain($field['display_label']);
    }
   }
}

/**
 * Implementation of hook_i18n_object_info().
 */
function field_display_label_i18n_object_info_alter(&$info) {
  if (!isset($info['field_instance'])) {
    return;
Kevin Miller's avatar
Kevin Miller committed
  }
  $info['field_instance']['string translation']['properties']['display_label'] = t('Display label');
}