Skip to content

Instantly share code, notes, and snippets.

@gheydon
Last active March 3, 2021 22:28
Show Gist options
  • Save gheydon/2d9e1637d7dd413c1aa2f41abab2e6ba to your computer and use it in GitHub Desktop.
Save gheydon/2d9e1637d7dd413c1aa2f41abab2e6ba to your computer and use it in GitHub Desktop.
Adding addition suggestions on a template based upon the referring entity.
<?php
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function example_theme_suggestions_file_link_alter(array &$suggestions, array $variables) {
/** @var \Drupal\file\Entity\File $file */
$file = $variables['file'];
try {
if ($file->_referringItem) {
/** @var EntityInterface $entity */
$entity = $file->_referringItem->getParent()->getParent()->getValue();
$suggestions[] = $variables['theme_hook_original'] . '__' . $entity->getEntityTypeId();
$suggestions[] = $variables['theme_hook_original'] . '__' . $entity->getEntityTypeId() . '__' . $entity->bundle();
}
}
catch (Exception $e) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment