I'm currently working on a project where I need to modify the meta tag values and/or the pattern being used to generate the tags. I've looked at the various alter hooks available, and neither of the hooks I looked at provided enough context for me to make the changes I wanted reliably.
Patch to follow which passes additional information through the following alter hooks:
- hook_metatag_pattern_alter
- hook_metatag_metatags_view
hook_metatag_pattern_alter
This currently passes the tokenised pattern, and the token data, but doesn't pass anything which tells you which metatag is being processed. If you want to use this hook to alter the meta tag pattern for a specific metatag (Which seems to be its intended use case?) then you have to check the pattern, and hope that only one metatag uses that pattern, and that it doesn't change. The patch attached also sends the metatag name as a third argument so implementations can check the metatag being processed to decide how/if to act.
hook_metatag_metatag_metatags_view
This currently passes the array of metatags that have been generated as well as the "instance" which tells you, for example, that you are generating tags for a node of type foo ("node:foo"), but it doesn't pass anything which tells you which object the tags are being generated for. You can probably check arg(x) to see what page is being viewed and work it out from that, but it seems cleaner if the hook provided the array of tokens that it is going to use, which then tells you what is being processed. The patch sends this as an additional third argument.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | add-more-context-to-alter-hooks-2307523-2.patch | 1.1 KB | leewillis77 |
Comments
Comment #1
leewillis77 commentedComment #2
damienmckennaThanks for the patch.
FYI when you upload a patch please change the status to "Needs review", that will trigger automated tests to ensure the patch can apply correctly, and notifies everyone that you've got some code for review.
Comment #3
damienmckennaComment #4
damienmckennaCommitted, though I did add notes to the corresponding sections of the API docs.