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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sachbearbeiter created an issue. See original summary.

guschilds’s picture

Hi 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 of hook_theme_suggestions_HOOK() in this module. Without passing more information to the definition of youtube_thumbnail, an implementation of youtube_theme_suggestions_youtube_thumbnail() would only have this information to create suggestions from:

  • The video ID (user input)
  • The title of the entity the video is on (passed for use with theming)
  • The image style the image will be rendered with (display setting)
  • If the image should be linked and where to (display setting)

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?

sachbearbeiter’s picture

Thanks 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 ...

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?

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)

guschilds’s picture

Status: Active » Needs review
FileSize
5.78 KB

Disclaimers:

  • I don't yet have enough Drupal 8 experience to know if this is the best way to do this (it probably isn't).
  • I think you have the information available to accomplish what you want with variants of the field template

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 . '__' . $field->get('field_type');
$original . '__' . $field->get('field_name');
$original . '__' . $field->get('entity_type') . '__' . $field->get('bundle');
$original . '__' . $field->get('entity_type') . '__' . $field->get('field_name');
$original . '__' . $field->get('entity_type') . '__' . $field->get('field_name') . '__' . $field->get('bundle');

($original is youtube_video or youtube_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.

sachbearbeiter’s picture

Thanks a lot - i will test this (give me a few days ...)

guschilds’s picture

Status: Needs review » Closed (won't fix)

Closing after a year of inactivity due to lack of interest. Please reopen if I'm mistaken.