3

I've scoured the documentation and I simply cannot find an answer to this question regarding naming convention for twig files given a tagged view with the override of a specific field. Let me create a scenario to explain the problem.

Given Viewname with tag viewtag with a block display and a fieldname field.

I can override the theme as follows

Original file --> Overridden file name

  • views-view-unformatted.html.twig --> views-view-unformatted--viewtag.html.twig
  • views-view-field.html.twig --> views-view-field--fieldname.html.twig
  • views-view-field.html.twig --> views-view-field--viewname--block--fieldname.html.twig
  • views-view-field.html.twig --> views-view-field--block--fieldname.html.twig

I cannot figure out how to override views-view-field for a specific fieldname by specifying the viewtag. The following names do not work:

  • views-view-field--viewtag--fieldname
  • views-view-field--viewtag--block--fieldname
  • views-view-field--fieldname--viewtag
  • views-view-field--block--fieldname--viewtag
  • views-view-field--block--viewtag--fieldname

I've run out of possible combinations and yet I'm simply not able to get the viewtags to override the specific field. Surely there has to be a way to do this given that you can use the viewname to override the templates. I'd rather avoid having 50 versions of a view template when 1 could do the same thing.

2
  • 1
    Have you enabled Twig debugging? That will give you all possible template names and you can even write custom theme hook suggestions if necessary. Commented May 12, 2016 at 13:52
  • I have twig debugging enabled. The problem is that it doesn't give anywhere near all of the possible naming conventions. Almost none of the naming conventions I have listed above which do work are listed on twig debugging. Commented May 12, 2016 at 21:35

3 Answers 3

1

I am able to use the tag to create template files for everything other than the specific field templates. – John Millnik

Your comment gave me the following thought: When you tag something, you tag the node and not the fields themselves. Fields do not have any tags, which makes me think that what you're trying to do is not possible.

4
  • This is not correct. I am able to use the tag to create template files for everything other than the specific field templates. Commented May 12, 2016 at 21:22
  • @JohnMillnik updated my answer.
    – No Sssweat
    Commented May 13, 2016 at 5:25
  • But you also are attributing the name to the view and you can modify the template via the name + display type. And views aren't nodes, are they? Either way I'm thinking this might actually just be an oversight on the views for Drupal 8. I'll follow up with developers to see if this is currently possible. It's certainly by design supposed to be possible. The point of tagging is to prevent twenty identical template files. Commented May 14, 2016 at 13:56
  • 1
    @JohnMillnik that's because name and display type are related/part of/assigned to the view. While tags are not related/part of/assigned to fields. I'll follow up with developers to see if this is currently possible. I highly doubt it, but let us know what you hear back.
    – No Sssweat
    Commented May 15, 2016 at 0:51
1

If I'm understanding your question correctly, you are looking to add a theme suggestion for a field based on a "viewtag".

I can't say I'm 100% sure what you mean by a "viewtag", but I think you are probably looking for hook_theme_suggestions_HOOK_alter or more specifically hook_theme_suggestions_views_view_field_alter().

More in for in Adding Theme Suggestions Programmatically For Views (Rows & Fields)

-1

I am pretty sure you cannot target a specific field in a view. Is this view being pulled into a global sidebar or into the content area of a specific content type? If it's being pulled into the content area then you can add the view block as an entity reference field in the content type itself and target the field that way, like targeting a field inside of a field. Let me give an example and explain.

Let's say you want track titles (field_track_title) and their albums (field_album) to appear on a bio page. (The bio page and the tracks are separate content types, bio page machine name being bio.) Create your view block displaying the track titles and albums as normal. Go ahead and place this block in a hidden region under block layout- this must be done in order to render it anywhere.

Then, in the bio content type, create a field referencing "other."

enter image description here

On the next screen select Block as the type of item to reference and set allowed number of values to 1.

When you get to the edit field screen you can select your newly created view block as the default (so you don't have to select it each time you create a new content type instance) and save. Make sure to place this field somewhere in your Manage Display screen.

Finally, to answer your question, you now can target this view's field because it renders as a field in the content type.

field--node--field_album--bio.html.twig

1
  • As mentioned - I am able to explicitly target any field by using several combinations including views-view-field--fieldname and views-view-field--viewname--displaytype--fieldname ... So I'm not sure this has anything to do with the actual question ;-) Commented May 12, 2016 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.