function ds_preprocess_field in Display Suite 8.2
Same name and namespace in other branches
- 8.4 ds.module \ds_preprocess_field()
- 8.3 ds.module \ds_preprocess_field()
- 7 modules/ds_extras/ds_extras.module \ds_preprocess_field()
Implements hook_preprocess_field().
1 string reference to 'ds_preprocess_field'
- ds_theme_registry_alter in ./
ds.module - Implements hook_theme_registry_alter().
File
- ./
ds.module, line 552 - Display Suite core functions.
Code
function ds_preprocess_field(&$variables) {
$entity_type = $variables['element']['#entity_type'];
$bundle = $variables['element']['#bundle'];
$view_mode = isset($variables['element']['#ds_view_mode']) ? $variables['element']['#ds_view_mode'] : $variables['element']['#view_mode'];
/* @var $entity_display EntityDisplayInterface */
$entity_display = Ds::getDisplay($entity_type, $bundle, $view_mode);
if ($entity_display && $entity_display
->getThirdPartySetting('ds', 'layout')) {
// Get the field name and field instance info - if available.
$field_name = $variables['element']['#field_name'];
$config = array();
static $field_settings = array();
if (!isset($field_settings[$entity_type][$bundle][$view_mode])) {
$f = array();
// Get third party settings for Core fields.
foreach ($entity_display
->getComponents() as $key => $info) {
if (!empty($info['third_party_settings']['ds']['ft'])) {
$f[$key]['ft'] = $info['third_party_settings']['ds']['ft'];
}
}
// Get third party settings for Display Suite fields.
$ds_fields_third_party_settings = $entity_display
->getThirdPartySetting('ds', 'fields');
if ($ds_fields_third_party_settings) {
$f += $entity_display
->getThirdPartySetting('ds', 'fields');
}
$field_settings[$entity_type][$bundle][$view_mode] = $f;
}
// Check if this field has custom output settings.
$variables['ds-config'] = array();
if (isset($field_settings[$entity_type][$bundle][$view_mode][$field_name]['ft'])) {
$config = $field_settings[$entity_type][$bundle][$view_mode][$field_name]['ft'];
$variables['ds-config'] = $config;
// When dealing with a field template we need to massage to values before
// printing to prevent layout issues.
if (isset($config['id']) && $config['id'] != 'default' && !empty($variables['ds-config']['settings'])) {
/* @var \Drupal\ds\Plugin\DsFieldTemplate\DsFieldTemplateInterface $layout_instance */
$layout_instance = \Drupal::service('plugin.manager.ds.field.layout')
->createInstance($config['id']);
if (isset($variables['element']['#object'])) {
$layout_instance
->setEntity($variables['element']['#object']);
}
$layout_instance
->massageRenderValues($variables['ds-config']['settings'], $config['settings']);
}
}
// CSS classes.
if (isset($config['settings']['classes'])) {
foreach ($config['settings']['classes'] as $class_name) {
if (isset($variables['element']['#object'])) {
$class_name = \Drupal::token()
->replace($class_name, array(
$entity_type => $variables['element']['#object'],
), array(
'clear' => TRUE,
));
}
$variables['attributes']['class'][] = $class_name;
}
}
// Alter the label if configured.
if (!$variables['label_hidden']) {
if (!empty($config['settings']['lb'])) {
$variables['label'] = t(Html::escape($config['settings']['lb']));
}
}
}
}