- Issue created by @drupaldope
- leymannx Berlin
Suggestion based on what? As you can see in your output you can use file-video.html.twig. What other suggestion based on what would you need on top?
- Status changed to Postponed: needs info
over 1 year ago 1:18am 6 April 2023 - Status changed to Active
over 1 year ago 1:20am 6 April 2023 - leymannx Berlin
Ah sorry, it's in the issue title. You want a template suggestion for the file based on the media's view mode.
yes, thank you :)
additionally to file-video.html.twig there should be things such as:
file-media-video.html.twig
file-media-viewmode-video.twigor similar
- leymannx Berlin
We would need to find a way to pass the view mode from the media entity down to the file. Not sure if that's possible easily.
- 🇪🇨Ecuador jwilson3
This can be done by passing down the view mode via data attributes from the field template preprocess and then removing the data attribute in the file_video template suggestion hook.
function HOOK_theme_suggestions_file_video_alter(array &$suggestions, array $variables) { // Check if there's a view mode available. if (!empty($variables['attributes'])) { /** @var \Drupal\Core\Template\Attribute $attrs */ $attrs = $variables['attributes']; if ($attrs->hasAttribute('data-view-mode')) { /** @var \Drupal\Core\Template\AttributeString $view_mode */ $view_mode = $attrs->storage()['data-view-mode']; $attrs->removeAttribute('data-view-mode'); // Sanitize the view mode name to use it in the template suggestion. $view_mode_sanitized = str_replace('.', '_', $view_mode); // Add the new suggestion with the view mode name. $suggestions[] = 'file_video__' . $view_mode_sanitized; } } } function HOOK_preprocess_field__file(&$variables) { if ( $variables['element']['#formatter'] == 'file_video' && !empty($variables['element']['#view_mode']) ) { foreach (Element::children($variables['element']) as $key => $value) { if ($variables['element'][$key]['#theme'] == 'file_video') { $attrs = &$variables['element'][$key]['#attributes']; $attrs->setAttribute('data-view-mode', $variables['element']['#view_mode']); } } } }