Skip to content

Instantly share code, notes, and snippets.

@welly
Created January 30, 2024 20:21
Show Gist options
  • Save welly/195d9d703707c61b09661220c745fd98 to your computer and use it in GitHub Desktop.
Save welly/195d9d703707c61b09661220c745fd98 to your computer and use it in GitHub Desktop.
Passing attributes between theme hooks
function some_theme_preprocess_paragraph(&$variables) {
$element = &$variables['elements'];
$paragraph = $variables['paragraph'];
if ($paragraph->getType() === 'image_gallery') {
$images = $paragraph->get('field_image_gallery');
foreach ($images->referencedEntities() as $image) {
$image->parent_paragraph = $paragraph;
}
}
}
function some_theme_theme_suggestions_media_alter(array &$suggestions, array $variables) {
$media = $variables['elements']['#media'];
if ($paragraph = $media->parent_paragraph) {
$suggestions[] = $variables['theme_hook_original'] . '__' . $media->bundle() . '__' . $paragraph->getType();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment