Skip to content

Instantly share code, notes, and snippets.

@rikki-iki
Last active February 7, 2023 01:47
Show Gist options
  • Save rikki-iki/6fe41e11ac59b3e1d07e to your computer and use it in GitHub Desktop.
Save rikki-iki/6fe41e11ac59b3e1d07e to your computer and use it in GitHub Desktop.
Useful theme suggestions (may use deprecated php)
<?php
use Drupal\block\Entity\Block;
use Drupal\block_content\BlockContentInterface;
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function HOOK_theme_suggestions_page_alter(array &$hooks, array &$vars): void {
$constrained = [
'page__user',
'page__sitemap',
'page__404',
'page__403',
'page__node__generate_preview_link',
'page__node__layout__discard_changes',
'page__node__layout__revert',
];
if (array_intersect($constrained, $hooks)) {
$hooks[] = 'page__constrained';
}
}
/**
* Implements hook_theme_suggestions_block_alter().
*/
function HOOK_theme_suggestions_block_alter(array &$hooks, array &$vars): void {
$original = $vars['theme_hook_original'];
$view_mode = null;
// View mode
if (isset($vars['elements']['#configuration']['view_mode'])) {
$view_mode = $vars['elements']['#configuration']['view_mode'];
$hooks[] = "{$original}__{$view_mode}";
}
if (isset($vars['elements']['#id'])) {
$block_id = $vars['elements']['#id'];
if (($block = Block::load($block_id)) && $block instanceof Block) {
// Region
$region = $block->getRegion();
$hooks[] = "{$original}__{$region}__{$vars['elements']['#base_plugin_id']}";
$hooks[] = "{$original}__{$region}__{$block_id}";
// Pass region through to hook_preprocess_block().
$vars['elements']['#region'] = $region;
// Custom theme_suggestions based on ID
switch ($block_id) {
case 'mainpagecontent':
case 'pagetitle':
$hooks[] = "{$original}__bare";
break;
}
}
}
// Custom Blocks (Bundles)
if (isset($vars['elements']['content']['#block_content'])) {
if (($block = $vars['elements']['content']['#block_content']) && $block instanceof BlockContentInterface) {
// Bundle type
if ($bundle = $block->bundle()) {
$hooks[] = "{$original}__{$bundle}";
if ($view_mode) {
$hooks[] = "{$original}__{$bundle }__{$view_mode}";
}
// Pass bundle through to hook_preprocess_block().
$vars['elements']['#bundle'] = $bundle;
}
}
}
// Custom theme suggestions based on plugin.
$plugin_id = $vars['elements']['#plugin_id'] ?? '';
if (str_starts_with($plugin_id, 'system_menu_block:footer-')) {
$hooks[] = "{$original}__footer_menu";
}
}
/**
* Implements hook_theme_suggestions_field_alter().
*/
function HOOK_theme_suggestions_field_alter(array &$hooks, array &$vars): void {
$original = $vars['theme_hook_original'];
$name = $vars['element']['#field_name'];
// View mode
if ($vars['element']['#view_mode']) {
$view_mode = $vars['element']['#view_mode'];
$hooks[] = "{$original}__{$view_mode}";
$hooks[] = "{$original}__{$view_mode }__{$name}";
}
// Name
$bare_names = [
'body',
'title',
'components',
];
// Hook
$bare_hooks = [
'media:image:hero:field_name',
'node:news:teaser:field_name',
];
$hook = implode(':', [
$vars['element']['#entity_type'],
$vars['element']['#bundle'],
$vars['element']['#view_mode'],
$vars['element']['#field_name'],
]);
// Suggest keeping field.html.twig very sparce of markup,
// then field--bare.html.twig has absolutely none.
if (in_array($name, $bare_names, TRUE) || in_array($hook, $bare_hooks, TRUE)) {
$hooks[] = "{$original}__bare";
}
}
/**
* Implements hook_theme_suggestions_menu_alter().
*/
function HOOK_theme_suggestions_menu_alter(array &$hooks, array &$vars): void {
$original = $vars['theme_hook_original'];
// These are present as we set them in hook_preprocess_block().
// @see https://gist.github.com/rikki-iki/f07f9a741597dfabecfd6f88ba69d435#file-drupal-8-preprocess-theme-L94
if (isset($vars['attributes']['region'])) {
$hooks[] = "{$original}__{$vars['attributes']['region']}";
}
if (isset($vars['attributes']['block_id'])) {
$block_id = str_replace(array('block-', '-'), array('', '_'), $vars['attributes']['block_id']);
$hooks[] = "{$original}__{$block_id}";
}
if (preg_match('/^footer-/', $vars['menu_name'])) {
$hooks[] = "{$original}__footer";
}
}
/**
* Implements hook_theme_suggestions_user_alter().
*/
function HOOK_theme_suggestions_user_alter(array &$hooks, array &$vars): void {
$original = $vars['theme_hook_original'];
$view_mode = $vars['elements']['#view_mode'];
$hooks[] = "{$original}__{$view_mode}";
}
/**
* Implements hook_theme_suggestions_taxonomy_term_alter().
*/
function HOOK_theme_suggestions_taxonomy_term_alter(array &$hooks, array &$vars): void {
$original = $vars['theme_hook_original'];
$term = $vars['elements']['#taxonomy_term'];
$hooks[] = "{$original}__{$term->bundle()}__{$vars['elements']['#view_mode']}";
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function HOOK_theme_suggestions_form_element_alter(array &$hooks, array &$vars): void {
$original = $vars['theme_hook_original'];
if ($vars['element']['#type'] === 'select') {
$hooks[] = "{$original}__select";
}
}
/**
* Implements hook_theme_suggestions_media_alter().
*/
function HOOK_theme_suggestions_media_alter(array &$hooks, array &$vars): void {
$original = $vars['theme_hook_original'];
$view_mode = $vars['elements']['#view_mode'] ?? 'default';
$bundle = $vars['elements']['#bundle'] ?? $vars['elements']['#media']->bundle->entity->id();
$bare_list = [
'hero',
'full',
];
if (in_array($view_mode, $bare_list, TRUE) || $bundle === 'file') {
$hooks[] = "{$original}__bare";
}
}
/**
* Implements hook_theme_suggestions_views_view_unformatted_alter().
*/
function HOOK_theme_suggestions_views_view_unformatted_alter(array &$hooks, array &$vars): void {
$original = $vars['theme_hook_original'];
/** @var \Drupal\views\ViewExecutable $view */
$view = $vars['view'];
$bare_list = [
'event_listing:event_listing_block',
'event_listing:past_event_listing_block',
'news_listing:news_listing_block',
];
if (in_array(sprintf('%s:%s', $view->id(), $view->current_display), $bare_list, TRUE)) {
$hooks[] = "{$original}__bare";
$hooks[] = "{$original}__{$view->id()}";
$hooks[] = "{$original}__{$view->id()}__{$view->current_display}";
}
}
@ummdorian
Copy link

This was super helpful, thanks!

@jorislucius
Copy link

Really helpful, especially the theme suggestions for Custom Blocks (Bundles), thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment