Hello,
i have the problem, that i need a different template structure by the context the youtube field is used in.
I hope i'm only confused ;)
Variant 1 for example of youtube-thumbnail.html.twig is
<div class="js-toggle-youtube toggle-youtube">
<div class="lazyYT" data-youtube-id="{{ video_id }}" data-modestbranding="1" data-rel="0" data-wmode="opaque" data-autohide="1" data-iv_load_policy="3" data-ratio="16:9">loading...</div>
</div>
{{ attach_library('oeb/lazyyt') }}
But i need also another variant ...
The only offered suggestion is
<!-- THEME HOOK: 'youtube_thumbnail' -->
<!-- BEGIN OUTPUT from 'themes/custom/foo/templates/misc/youtube-thumbnail.html.twig' -->
Via kint i find no data in the preprocessing function, that i can use for custom suggestions.
For example variables of the parent field etc.
Is there a possibility to get some information of the context?
Or is it possible to ignore the youtube templates and use only the field output (i think, there i get the right suggestions)?
Or would it maybe make sense to bring in "View modes" or default suggestions like:
youtube-thumbnail--field-foo.html.twig
Thanks and regards
SB
Comment | File | Size | Author |
---|---|---|---|
#4 | youtube-template-suggestions-2646128-4.patch | 5.78 KB | guschilds |
Comments
Comment #2
guschilds CreditAttribution: guschilds at Chromatic commentedHi sachbearbeiter,
Template suggestions in Drupal 8 are defined with hook_theme_suggestions_HOOK(). Some examples:
Those functions create suggestions based on the
$variables
variable. Right now there is no implementation ofhook_theme_suggestions_HOOK()
in this module. Without passing more information to the definition ofyoutube_thumbnail
, an implementation ofyoutube_theme_suggestions_youtube_thumbnail()
would only have this information to create suggestions from:The tough part here is deciding what suggestions would make sense and figuring out how to pass that information through. Suggestions based on field display settings would be easy because we already have that information. Things like field name, view mode, bundle (content type), and entity type would have to be passed along. I'd rather not create more suggestions than would be useful.
I'm curious, did you determine if you have the information needed in the field template? Did it make sense to use that or would it have made more sense to use a suggestion provided by this module?
Comment #3
sachbearbeiter CreditAttribution: sachbearbeiter commentedThanks for the answer.
I'm afraid that my use case is the most complicate one:
Context one:
- Parent entity: paragraph (Paragraphs module)
- Parent bundle: para_youtube_video
- Field: field_youtube
- Node bundle that uses this paragraph: slideshow
Context two:
- Parent entity: paragraph (Paragraphs module)
- Parent bundle: para_youtube_video
- Field: field_youtube
- Node bundle that uses this paragraph: video
So maybe for me it would make more sense to build a custom formatter ...
And one thing i do not understand:
{{item.content}} is printed by default in the video field.html.twig template - if i go deeper into the data structure like {{item.content.foo}} instead, the youtube-thumbnail.html.twig is not used anymore?
But other maybe more typical scenarios:
Context one:
- Parent node bundle: slideshow
- Field: field_youtube
Context two:
- Parent node bundle: video
- Field: field_youtube
youtube-thumbnail--[bundle].html.twig
youtube-thumbnail.html.twig (or youtube-video.html.twig as the other variant)
*********************************
Context one:
- Parent node bundle: slideshow
- Field: field_youtube_one
Context two:
- Parent node bundle: video
- Field: field_youtube_two
youtube-thumbnail--[field_name].html.twig
youtube-thumbnail.html.twig (or youtube-video.html.twig as the other variant)
*********************************
Maybe something like this a result:
youtube-thumbnail--[bundle]--[field_name].html.twig
youtube-thumbnail--[field_name].html.twig
youtube-thumbnail--[bundle].html.twig
youtube-thumbnail.html.twig (or youtube-video.html.twig as the other variant)
Comment #4
guschilds CreditAttribution: guschilds at Chromatic commentedDisclaimers:
That said, I believe the patch I've attached provides the suggestions you're looking for. They match field template suggestions because they're based off of system_theme_suggestions_field() and are:
(
$original
isyoutube_video
oryoutube_thumbnail
.)With this patch applied to the 8.x-1.x branch, I'm seeing the proper list of suggestions in the theme debug output.
Please try the patch and let me know how it goes.
Comment #5
sachbearbeiter CreditAttribution: sachbearbeiter commentedThanks a lot - i will test this (give me a few days ...)
Comment #6
guschilds CreditAttribution: guschilds at Chromatic commentedClosing after a year of inactivity due to lack of interest. Please reopen if I'm mistaken.