-
-
Save paboden/912eab9c670f0223f15ebd0e76ff9419 to your computer and use it in GitHub Desktop.
Drupal 8 Paragraphs: Add a theme suggestion based on the paragraph and the type of the parent node / 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 theme_suggestions_HOOK_alter(). | |
| */ | |
| function mytheme_theme_suggestions_paragraph_alter(array &$suggestions, array $variables) { | |
| /** @var \Drupal\paragraphs\ParagraphInterface $paragraph */ | |
| $paragraph = $variables['elements']['#paragraph']; | |
| /** @var \Drupal\Core\Entity\ContentEntityInterface $parent */ | |
| $parent = $paragraph->getParentEntity(); | |
| $suggestions[] = 'paragraph__' . $paragraph->bundle() . '__' . $parent->bundle(); | |
| } | |
| /** | |
| * Implements hook_theme_suggestions_HOOK_alter(). | |
| */ | |
| function mythme_theme_suggestions_paragraph_alter(&$suggestions, $variables) { | |
| $paragraph = $variables['elements']['#paragraph']; | |
| $suggestions[] = 'paragraph__' . $paragraph->bundle() . '__' . $paragraph->view_mode; | |
| $parent = $paragraph->getParentEntity(); | |
| if ($parent) { | |
| $suggestions[] = 'paragraph__' . $parent->bundle() . '__' . $paragraph->bundle(); | |
| $suggestions[] = 'paragraph__' . $parent->bundle() . '__' . $paragraph->bundle() . '__' . $paragraph->view_mode; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment