You are here

function ds_preprocess_field in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 ds.module \ds_preprocess_field()
  2. 8.2 ds.module \ds_preprocess_field()
  3. 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 708
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);

  // Check if this field is being rendered as a layout builder FieldBlock
  // @see Drupal\layout_builder\Plugin\Block\FieldBlock::build();
  $is_layout_builder = !empty($variables['element']['#third_party_settings']['layout_builder']['view_mode']) && \Drupal::config('ds.settings')
    ->get('ft-layout-builder');
  if ($entity_display && $entity_display
    ->getThirdPartySetting('ds', 'layout') || $is_layout_builder) {

    // Get the field name and field instance info - if available.
    $field_name = $variables['element']['#field_name'];
    $config = [];
    if ($is_layout_builder && !empty($variables['element']['#third_party_settings']['ds']['ft'])) {
      $field_settings = [];
      $field_settings[$entity_type][$bundle][$view_mode][$field_name]['ft'] = $variables['element']['#third_party_settings']['ds']['ft'];
    }
    else {
      static $field_settings = [];
      if (!isset($field_settings[$entity_type][$bundle][$view_mode])) {
        $f = [];

        // 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'] = [];
    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, [
            $entity_type => $variables['element']['#object'],
          ], [
            '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']));
      }
    }
  }
}