Last active
March 3, 2021 22:28
-
-
Save gheydon/2d9e1637d7dd413c1aa2f41abab2e6ba to your computer and use it in GitHub Desktop.
Adding addition suggestions on a template based upon the referring entity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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