You are here

function ds_theme_suggestions_field_alter in Display Suite 8.2

Same name and namespace in other branches
  1. 8.4 ds.module \ds_theme_suggestions_field_alter()
  2. 8.3 ds.module \ds_theme_suggestions_field_alter()

Implements hook_theme_suggestions_HOOK_alter().

The suggestion alters for for field templates.

File

./ds.module, line 631
Display Suite core functions.

Code

function ds_theme_suggestions_field_alter(&$suggestions, $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'];
    $field_theme_function = \Drupal::config('ds.settings')
      ->get('ft-default');
    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;
    }
    $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);

    // Check if this field has custom output settings.
    $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'];
    }

    // Initialize suggestion name.
    $suggestion = '';

    // Determine the field template. In case it's something different.
    if (isset($config['id']) && $config['id'] != 'default') {
      $layout_instance = \Drupal::service('plugin.manager.ds.field.layout')
        ->createInstance($config['id']);

      // Either it uses the function.
      $suggestions[] = 'field__' . $layout_instance
        ->getThemeFunction();

      // Or the template file(s).
      $suggestion = 'field__' . $config['id'];
    }
    elseif ($field instanceof FieldConfigInterface && ($theme_function = $field
      ->getThirdPartySetting('ds', 'ds_field_template', '')) && !empty($theme_function)) {

      // Either it uses the function.
      $suggestions[] = 'field__theme_ds_field_' . $theme_function;

      // Or the template file(s).
      $suggestion = 'field__' . $theme_function;
    }
    elseif (!empty($field_theme_function)) {
      $suggestions[] = 'field__theme_ds_field_' . $field_theme_function;

      // Or the template file(s).
      $suggestion = 'field__' . $field_theme_function;
    }
    if (!empty($suggestion)) {
      $suggestions[] = $suggestion;
      $suggestions[] = $suggestion . '__' . $field_name;
      $suggestions[] = $suggestion . '__' . $variables['element']['#bundle'];
      $suggestions[] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
      $suggestions[] = $suggestion . '__' . $variables['element']['#entity_type'] . '__' . $field_name;
      $suggestions[] = $suggestion . '__' . $variables['element']['#entity_type'] . '__' . $variables['element']['#bundle'];
      $suggestions[] = $suggestion . '__' . $variables['element']['#entity_type'] . '__' . $field_name . '__' . $variables['element']['#bundle'];
    }

    // Custom DS fields name may contain colon separators or dashes; replace it
    // with "__" or "_" to ensure suggestions are compatible with file names on
    // all systems.
    foreach ($suggestions as $key => $suggestion) {
      $suggestions[$key] = str_replace([
        ':',
        '-',
      ], [
        '__',
        '_',
      ], $suggestion);
    }
  }
}