Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I can use view mode template suggestions only after I specify content type, eg: node--contenttype--viewmode.tpl.php . I am trying to use node templates per view mode for all content types, eg: node--viewmode.tpl.php but with no results.
Comment | File | Size | Author |
---|---|---|---|
#4 | template_suggestion_view_modes-1300818-4.patch | 1.55 KB | Anonymous (not verified) |
#3 | 1300818-3-template-suggestions.patch | 1.02 KB | Manuel Garcia |
Comments
Comment #1
lukpe CreditAttribution: lukpe commentedOK, I'm not a dev, but if anyone is looking for this kind of functionality, here is a solution - replace
function entity_view_mode_preprocess
with this code (exclulding opening and closing php tags):Last line implements/hacks in functionality I was looking for.
PS. Sorry but I don't know how to create patch (yet).
Comment #2
lance.gliser CreditAttribution: lance.gliser commentedI would also like to target just the node and view type at times. I got around it by adding only:
to the preprocess function.
Comment #3
Manuel Garcia CreditAttribution: Manuel Garcia commentedThis would indeed be nice to have, so as to reuse the same template for different bundles.
Find attached the patch based on #1 & #2
Comment #4
Anonymous (not verified) CreditAttribution: Anonymous commentedThe code in #2 is broken. After checking for the existence of that value in the array, it searches for that value again. This has the effect of always adding the suggestion in the same place. There's a patch that addresses this bug as it already exists in the module. #1831766
I've rewritten the patch in #3 to apply on the current dev release. It also adds the {$entity_type}__{$view_mode} suggestion as the lowest priority, so it shouldn't disrupt existing themes that depend on the order provided by this function.
Comment #5
JurriaanRoelofs CreditAttribution: JurriaanRoelofs commentedTest 4 works well. Minor note: patch file name should start with project name.
Comment #6
Dave ReidMy question is since both view mode and bundle are strings, how can we prevent collisions when both view mode and bundle are the same string? I'm not sure how useful a template for an entity type and view mode but is bundle-agnostic since by nature view modes are very bundle specific for their configuration and don't share anything between different bundles.
Comment #7
Dave ReidComment #8
JurriaanRoelofs CreditAttribution: JurriaanRoelofs commentedThis is not the case if you embrace view modes as a first class citizen in your application, view modes are an excellent analogy to design patterns of well crafted application designs.
I think the best way to prevent collision is to add a not in the project page and README, there are more points in Drupal where you just give stuff the same name so I think it's a best practice.
Comment #9
Anonymous (not verified) CreditAttribution: Anonymous commentedI have a view mode called "slide" which is how nodes are formatted when they appear in a jQuery Cycle slideshow. My theme suggests a theme as 'node__slide' which causes Drupal to use node--slide.tpl.php.
This template prints specific variables including from $content for fields. Actually it's just a "main image" field, which almost every content type will have.
We cannot prevent collisions, but collisions can be managed with either with more specific templates to handle the conditions where the wrong template is selected or the theme's hook_preprocess_node function can override or force the theme_hook_suggestion(s).
I do think it's a potential problem because of the abundance of bundles and view modes for nodes. Someone might create a "teaser" or "print" content type. (Or "slide," which I've done, and the collision was perfectly acceptable!)
I've almost convinced myself that this kind of unpredictability isn't worth it. The right way to do this is to implement this in your theme:
or even:
Comment #10
Dave ReidShould we mark this as won't fix then?
Comment #11
Anonymous (not verified) CreditAttribution: Anonymous commentedYep.
Comment #13
goldenboy CreditAttribution: goldenboy commented4: template_suggestion_view_modes-1300818-4.patch queued for re-testing.
Comment #15
eelkeblokThe reason to close this as won't fix seems to be well thought-out. When re-opening (by supplying new versions of the patch or otherwise), please also give clear motivation why you still feel this needs to be added to this module.