field_display_label.module in Field Display Label 8
Same filename and directory in other branches
Main file for Field Display Label module.
File
field_display_label.moduleView source
<?php
/**
* @file
* Main file for Field Display Label module.
*/
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter() for field_config_edit_form().
*/
function field_display_label_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state) {
$field = $form_state
->getFormObject()
->getEntity();
if (!isset($field)) {
return;
}
$form['display_label'] = [
'#type' => 'textfield',
'#title' => t('Display label'),
'#description' => t('A separate label for viewing this field. Leave blank to use the default field label.'),
'#weight' => isset($form['label']['#weight']) ? $form['label']['#weight'] + 1 : 0,
'#default_value' => !empty($field
->getThirdPartySetting('field_display_label', 'display_label')) ? $field
->getThirdPartySetting('field_display_label', 'display_label') : '',
];
$form['actions']['submit']['#submit'][] = 'field_display_label_form_field_config_edit_form_submit';
}
/**
* Form submission handler for form_field_config_edit_form_alter.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function field_display_label_form_field_config_edit_form_submit(array $form, FormStateInterface $form_state) {
$field = $form_state
->getFormObject()
->getEntity();
$form_fields =& $form_state
->getValues();
// If the label is entered update settings.
if ($form_fields['display_label']) {
$field
->setThirdPartySetting('field_display_label', 'display_label', $form_fields['display_label']);
$field
->save();
}
else {
$field
->unsetThirdPartySetting('field_display_label', 'display_label');
$field
->save();
}
}
/**
* Implements hook_preprocess_field().
*/
function field_display_label_preprocess_field(&$variables) {
$element = $variables['element'];
$entity = $element['#object'];
if (empty($entity)) {
return;
}
$field_definition = $entity
->getFieldDefinition($element['#field_name']);
if ($field_definition instanceof ThirdPartySettingsInterface) {
$definition = $field_definition
->getThirdPartySetting('field_display_label', 'display_label');
if (isset($definition) && !empty($definition)) {
$variables['label'] = $definition;
}
}
}
Functions
Name | Description |
---|---|
field_display_label_form_field_config_edit_form_alter | Implements hook_form_FORM_ID_alter() for field_config_edit_form(). |
field_display_label_form_field_config_edit_form_submit | Form submission handler for form_field_config_edit_form_alter. |
field_display_label_preprocess_field | Implements hook_preprocess_field(). |