Hi,
I try to override the output of the plain format. If I copy the address-plain.html.twig file in the template folder of may custom theme, I'm able to make my changes.
Since I don't want to override the output for all address fields of all content types, I tried to rename the template to something like address-plain--contenttype.html.twig or myfieldname--address-plain.html.twig
I tried various other names, recommended by the Twig developer tools, but none of them worked. What template name do I need to choose, if I want to change the output only for a specific address field of a specific content type?
Thanks for suppurt
Filburt
Comments
Comment #2
bojanz commentedNo theme suggestions are provided. We have a bug.
Ideally we would have field, entity bundle, view mode suggestions.
Comment #3
validoll commentedPatch to add suggestions for 'address_plain'.
Comment #4
validoll commentedFix previous patch.
Suggestions needs to start with theme hook name.
Comment #5
kpolymorphic commentedTested out patch #3 but it did not work at all. Patch #4 did the trick, thanks!
Comment #6
kpolymorphic commentedMarking RTBC
Comment #7
NewZeal commentedHow about making it so we can use the view mode as well as the entity type and bundle? Modified patch attached. Thanks.
Comment #9
validoll commented@new-zeal please provide an interdiff.
Comment #10
dunot commentedOn D8.5.x-dev with commerce-8.x-2.x-dev #4 и #7 doesn't work.
i've tried address_plain__address.html.twig.
Comment #11
neetu morwani commentedThanks for the work @new-zeal. I applied the patch manually and I am able to leverage theme suggestions functionality with address field now.
Rerolled the existing patch so that it is compatible with the latest version of D8.5 and passes testing.
Moving it for further review.
Comment #12
neetu morwani commentedComment #13
bojanz commented1. It's odd that we're hardcoding address_plain__. We should be using $variables['theme_hook_original'].
Here's a commerce example, for an entity template:
2. It makes no sense to provide a suggestion for the field type, we always have the same field type ("address").
In general we need to decide which suggestions we want to provide. I'll discuss that with the team tomorrow.
Comment #14
kaythay commentedHere's another patch to keep this going.
Comment #15
hstandaert commentedRemoved the
/docrootprefix in the paths.Comment #16
mpp commentedThanks @hstandaert for your patch and welcome to d.o!
Comment #17
hstandaert commentedThanks @mpp! I added the patch again since something seems to have gone wrong the first time.
Comment #18
mpp commentedComment #19
dwwSorry, this no longer applies.
I notice #17 is using paths like so:
a/modules/contrib/address/address.module(e.g. from inside a fully built site). Patches need to be relative to the project's own git repo, not a site. E.g.a/address.moduleComment #20
dwwLike so. Didn't look at the patch yet, but hopefully the bot will like this.
Comment #21
dwwLooked more closely at the patch. I found some concerns:
These are unneeded.
$field(fromgetFieldDefinition()can directly tell you the entity type ID and bundle.Per the sample code from @bojanz above, this #view_mode wants to be "sanitized" before use in a theme suggestion.
Generally, I'm not totally sure these specific suggestions make sense like this. I'd like to see more thought put into these.
These are pointless, since
FormatterBasealready has this.Since we're inserting this into the available variables for the address-plain.html.twig template, we should also update the doc comment in that template to document that these are available for anyone who wants to use them.
Thanks,
-Derek
Comment #22
dwwI needed this to finally solve #2838509: Allow a field formatter to output names and codes for all subdivisions (e.g. for use with search indexing) for a site I’m building. ;)
Attached patch fixes everything from #21.
Re: #21.3: I looked at both
node_theme_suggestions_node()andsystem_theme_suggestions_field()for inspiration on what suggestions to provide and in what order. I think this is a pretty solid list:While I was looking closely at all this again, I didn’t love
#elementas a template variable with nested values for the address field and the view mode. Seems potentially confusing with form / render “element”, etc. If anything, I’d maybe call it#contextor something. But thenode.html.twigtemplate gets both node entity object and view_mode as separate variables. So I went with that approach here, too. This also made it easier to document what’s going on in address-plain.html.twig (#21.5).Finally, I added test coverage by extending
tests/src/Kernel/Formatter/AddressPlainFormatterTest.php(inspired bycore/modules/node/tests/src/Functional/NodeTemplateSuggestionsTest.php). So I’m attaching a test-only that (obviously) fails, and the full patch.Any final reviews / concerns / objections before I commit this?
Thanks!
-Derek
Comment #23
dwwp.s. This would be good to get in before 8.x-1.8
Comment #24
dwwIt seems
assertEqual()is deprecated (although only for D10), so we might as well avoid using it now.Comment #25
mpp commentedThanks, rtbc!
Comment #26
bojanz commentedLooks good!
Comment #28
dwwGreat, thanks for the reviews!
Committed and pushed to 8.x-1.x.
Cheers,
-Derek
Comment #30
dwwNote: we all completely forgot that
'#theme' => 'address_plain',is not only used for address fields. This issue broke things for address form elements and other render arrays that aren't necessarily a Field API field. :( See #3129410: Error in hook_theme suggestions