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.
Currently, we're building the HTML for $header
of Spotlight widgets entirely in code in the hook_field_formatter_view()
. Let's move that to a theme function (ideally, with a template), so that it'd be easy for themers to override and change how the spotlight is rendered.
Comments
Comment #1
dsnopekActually, it's just the
$header
variable, but it's still bad! Updating the issue summary.Comment #2
Andrew Edwards CreditAttribution: Andrew Edwards commentedHere's a first attempt at a patch for making the spotlight themable.
Here's what I did:
1) Split the spotlight JS out into a new file panopoly-widgets-spotlight.js
2) Added panopoly_spotlight_header and panopoly_spotlight_footer to panopoly_widgets_theme() in panopoly_widgets.module
3) Added two new theme functions to panopoly_widgets.spotlight.in (corresponding to 2 above)
4) Moved spotlight wrapper markup stuff from panopoly_widgets_field_formatter_view into the new theme function theme_panopoly_spotlight_header
note: theme_panopoly_spotlight_footer is there just in case it's needed (I need it for my implementation of the Bootstrap slideshow!).
Comment #3
Andrew Edwards CreditAttribution: Andrew Edwards commentedHere's the patch above without 1) Split the spotlight JS out...
Item one now has it's own issue: #2526330: Split spotlight JS into it's own file
Comment #4
Andrew Edwards CreditAttribution: Andrew Edwards commentedComment #5
Andrew Edwards CreditAttribution: Andrew Edwards commentedComment #6
dsnopekThanks for the patch!
I think having theme functions for header and footer is a little awkward. Could we instead have a 'panopoly_spotlight_wrapper' theme function? I wonder if we could just do something like:
... and then it would be responsible for creating any header and footer, as well as calling
render()
to render the slides. This would also allow us to get of the code that puts the header before the markup on the first slide, which has always bothered me. :-)Ideally, I'd also like to convert this from a theme function to a theme template, but we can look at that once we have the overall approach in place.
Comment #7
Andrew Edwards CreditAttribution: Andrew Edwards commentedTotally agreed. The header markup before the first slide was bothering me too.
I'll have a go at another patch using #theme_wrappers
Comment #8
Andrew Edwards CreditAttribution: Andrew Edwards commentedComment #9
Andrew Edwards CreditAttribution: Andrew Edwards commentedOk. How's this one?
I've:
1) Created a new render array for the spotlight, which uses #theme
2) Added the Spotlight items (slides) to the spotlight render array
3) Moved the markup for the Spotlight wrapper to a new #theme function theme_render_panopoly_spotlight
4) Rendered the Spotlight items from the #theme function theme_render_panopoly_spotlight
I didn't go for #theme_wrappers in the end because (I think) #theme_wrappers expects children to already be rendered. Also #2042285: #theme_wrappers should be able to have a unique set of variables per wrapper hook , which made me think it was better/fine to go with #theme.
$element[0] is used in panopoly_widgets_field_formatter_view() because of the recommendation here for displaying all markup within a single item: https://api.drupal.org/api/drupal/modules!field!field.api.php/function/h...
Comment #10
Andrew Edwards CreditAttribution: Andrew Edwards commentedComment #11
dsnopekThanks! I haven't had a chance to test it yet, but the patch is looking good. :-)
A couple minor points of review:
Let's call the theme function 'panopoly_spotlight_wrapper'. All our theme functions should be prefixed with the name of the module, and this is "wrapping" the spotlight slides which I think makes sense.
I'm not sure about putting this all under
$element[0]
. Look at the examples on the API page that you linked, when the elements can be rendered individually, its always putting the individual items under$element[$delta]
and in this case, the items can be rendered individually.Could we do it like:
This also changes from 'child' to 'slides', so the theme function would need to be updated too, but I think 'slides' is a little easier to understand for the themer.
Thanks again for your work on this!
Comment #12
Andrew Edwards CreditAttribution: Andrew Edwards commentedOk! Thanks for reviewing.
1) render_panopoly_spotlight changed to panopoly_spotlight_wrapper
2) 'child' changed to 'slides'
Changing $element[0] to $element didn't work for me.
I think it's because hook_field_formatter_view expects for it's return value: 'A renderable array for the $items, as an array of child elements keyed by numeric indexes starting from 0.'
I've put a comment in the code so people know why $element[0] was used.
Comment #13
Andrew Edwards CreditAttribution: Andrew Edwards commentedComment #14
dsnopekThanks for the updated patch! It looks good. :-)
Ok, thanks, your comment and explanation make sense - I'm cool with this now. :-)
One super minor nitpicky thing:
Can you wrap comments to 80 characters? Thanks!
I still have to do some manual testing of the patch (so far I've just been doing code reviews) and I'd still really like to convert these from theme functions to templates, but we can address that in a follow-up issue as to not hold this one up.
Comment #15
Andrew Edwards CreditAttribution: Andrew Edwards commentedNo probs! Here's a patch with the comment wrapped to 80 characters.
Thanks again for all your hard work on Panopoly :)
Comment #17
dsnopekThanks! This worked great in my testing, so, committed. :-)
I created a follow-up issue about converting the theme functions to templates: #2532238: Convert spotlight theme functions into templates
Comment #18
dsnopekUpdated title to reflect that this went to a function, not a template.