You are here

function twigsuggest_theme_suggestions_layout_alter in Twig Template Suggester 8

Implements hook_theme_suggestions_HOOK_alter() for layout templates.

Fix suggestion of entity type specific layout templates.

File

./twigsuggest.module, line 74
Twig Template Suggester module hook implementations.

Code

function twigsuggest_theme_suggestions_layout_alter(array &$suggestions, array $variables) {

  // Sometimes this can be helpful but othertimes apparently not so this hook
  // is disabled by default but can be enabled in settings.php:
  // $config['twigsuggest.settings']['alternate_ds_suggestions'] = TRUE;.
  if (!\Drupal::config('twigsuggest.settings')
    ->get('alternate_ds_suggestions')) {
    return;
  }

  // Although this appears to be done by default it's actually broken, as seen
  // in this bug report: https://www.drupal.org/project/drupal/issues/2881195
  // Apparently using two underscores/dashes will make it work despite the
  // double-listing (layout--onecol is there twice) still happening.
  if (isset($variables['content']) && is_array($variables['content']) && isset($variables['content']['#ds_configuration']) && $variables['theme_hook_original'] != 'ds_entity_view') {
    $layout_id = $variables['content']['#ds_configuration']['layout']['id'];
    $layout_id_len = strlen($layout_id);
    foreach ($suggestions as $key => $suggestion) {
      if (strpos($suggestion, $layout_id) === 0) {
        $base_suggest = str_replace('_', '__', $layout_id);
        $suggestions[$key] = substr_replace($suggestion, $base_suggest, 0, $layout_id_len);
      }
    }
  }
}