fences.module in Fences 8.2
Same filename and directory in other branches
Alter wrapping markup of fields.
File
fences.moduleView source
<?php
/**
* @file
* Alter wrapping markup of fields.
*/
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FormatterInterface;
use Drupal\fences\TagManagerInterface;
/**
* Implements hook_theme_registry_alter().
*/
function fences_theme_registry_alter(&$theme_registry) {
// Only override the field template if it came from core. Allow themes that
// have overridden the field template to take precedence.
if (strpos($theme_registry['field']['path'], 'core') !== 0) {
return;
}
$module_path = drupal_get_path('module', 'fences');
$theme_registry['field']['path'] = $module_path;
}
/**
* Implements hook_field_formatter_third_party_settings_form().
*/
function fences_field_formatter_third_party_settings_form(FormatterInterface $plugin, FieldDefinitionInterface $field_definition, $view_mode, $form, FormStateInterface $form_state) {
$settings['fences'] = [];
$invisible_on_none = function ($name) {
return [
'invisible' => [
'[name$="' . $name . ']"]' => [
'value' => TagManagerInterface::NO_MARKUP_VALUE,
],
],
];
};
$tag_options = \Drupal::service('fences.tag_manager')
->getTagOptions();
$settings['fences'] = [
'#type' => 'fieldset',
'#title' => t('Fences'),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
];
$settings['fences']['fences_field_tag'] = [
'#title' => t('Field Tag'),
'#type' => 'select',
'#options' => $tag_options,
'#default_value' => $plugin
->getThirdPartySetting('fences', 'fences_field_tag', 'div'),
];
$settings['fences']['fences_field_classes'] = [
'#title' => t('Field Classes'),
'#type' => 'textfield',
'#default_value' => $plugin
->getThirdPartySetting('fences', 'fences_field_classes'),
'#states' => $invisible_on_none('fences_field_tag'),
];
$settings['fences']['fences_field_item_tag'] = [
'#title' => t('Field Item Tag'),
'#type' => 'select',
'#options' => $tag_options,
'#default_value' => $plugin
->getThirdPartySetting('fences', 'fences_field_item_tag', 'div'),
];
$settings['fences']['fences_field_item_classes'] = [
'#title' => t('Field Item Classes'),
'#type' => 'textfield',
'#default_value' => $plugin
->getThirdPartySetting('fences', 'fences_field_item_classes'),
'#states' => $invisible_on_none('fences_field_item_tag'),
];
$settings['fences']['fences_label_tag'] = [
'#title' => t('Label Tag'),
'#type' => 'select',
'#options' => $tag_options,
'#default_value' => $plugin
->getThirdPartySetting('fences', 'fences_label_tag', 'div'),
];
$settings['fences']['fences_label_classes'] = [
'#title' => t('Label Classes'),
'#type' => 'textfield',
'#default_value' => $plugin
->getThirdPartySetting('fences', 'fences_label_classes'),
'#states' => $invisible_on_none('fences_label_tag'),
];
return $settings['fences'];
}
/**
* Implememts hook_preprocess_field().
*/
function fences_preprocess_field(&$vars) {
// Default to showing all markup.
$vars['display_label_tag'] = TRUE;
$vars['display_field_tag'] = TRUE;
$vars['display_item_tag'] = TRUE;
if (isset($vars['element']['#third_party_settings']['fences'])) {
$fences_config = $vars['element']['#third_party_settings']['fences'];
$vars['display_label_tag'] = $fences_config['fences_label_tag'] !== TagManagerInterface::NO_MARKUP_VALUE;
$vars['display_field_tag'] = $fences_config['fences_field_tag'] !== TagManagerInterface::NO_MARKUP_VALUE;
$vars['display_item_tag'] = $fences_config['fences_field_item_tag'] !== TagManagerInterface::NO_MARKUP_VALUE;
$vars['label_tag'] = $fences_config['fences_label_tag'];
$vars['field_tag'] = $fences_config['fences_field_tag'];
$vars['field_item_tag'] = $fences_config['fences_field_item_tag'];
$vars['attributes']['class'][] = $fences_config['fences_field_classes'];
$vars['title_attributes']['class'][] = $fences_config['fences_label_classes'];
if (!empty($fences_config['fences_field_item_classes'])) {
foreach ($vars['items'] as $field_item) {
$field_item['attributes']
->addClass($fences_config['fences_field_item_classes']);
}
}
}
}
Functions
Name | Description |
---|---|
fences_field_formatter_third_party_settings_form | Implements hook_field_formatter_third_party_settings_form(). |
fences_preprocess_field | Implememts hook_preprocess_field(). |
fences_theme_registry_alter | Implements hook_theme_registry_alter(). |