Problem/Motivation
Metatag D7 has #2143979: Allow Views meta tags to "Use replacement tokens from the first row" we should port it.
Proposed resolution
The token form and the tokenization logic can be lifted wholesale from TokenizeAreaPluginBase, one problem appears though: while Views caches results, it does cache the rendered fields necessary for tokenization. It doesn't need that because the render cache will store the tokenized, rendered results. However, page attachments are not cached because of #2478393: hook_page_(attachments|attachments_alter|top|bottom)() should specify the right cacheability metadata. Supplying our own cache plugin is not possible because we would need one for each existing cache plugin. So the patch decorates the views cache plugin manager in order to return a class wrapping the cache plugin.
Remaining tasks
User interface changes
New tokenization options in the views form.
API changes
Data model changes
Original issue summary
In a Views metatag options, you can use global tokens from the Token module. But that doesn't do you all that much good in a view; what you really need access to are Views' own replacement tokens for fields and arguments. This way you can use anything you can get at in your View in your metadata. The Metatag settings should have a "Use replacement tokens from the first row" checkbox, the same way "Global: Text area" fields do in headers and footers.
(D8 port of #2143979: Allow Views meta tags to "Use replacement tokens from the first row")
Issue fork metatag-2952229
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 8.x-1.x
compare
- 2952229-allow-views-meta
changes, plain diff MR !5
Comments
Comment #2
damienmckennaComment #3
lmpur commentedAre there any plans to implement this fix soon?
Comment #4
damienmckennaI do not have a client/project which currently needs this, but I'll happily review patches if someone has the time to write it.
Comment #5
josh.stewart commented@DamienMcKenna Any chance you've had a client/project which needed this in the past few months? Asking for a friend.
Comment #6
damienmckennaNone yet.
Comment #7
menelai commentedNeed this feature.
Comment #8
solide-echt commentedI've given this some thought but need some guidance - at least. The current class for this setting is based on a display_extender plugin. However, in views core the TokenizeAreaPluginBase class extends AreaPluginBase so we can not simply reuse that class. IMHO tokens in views would be better handled as a trait but that seems way out of scope for this - and beyond my OOP skills too ;-)
Anyway, I simply copied the functions from TokenizeAreaPluginBase to MetatagDisplayExtender but this wreaks havoc on the jQuery. The checkbox is shown but no description of field tokens is toggled and the submission returns a 500 error. Does this mean I'm on the wrong track or simply I need to put in more effort to debug what's going wrong with the jQuery - yikes ;-)
Kind regards,
Eric
Comment #9
dajka commentedThis would be a very nice to have feature! One up from me ;)
Comment #10
dajka commentedActually... This issue is the same as this one... https://www.drupal.org/project/metatag/issues/2962890 is that right? in case yes, can we merge them?
Comment #11
dajka commentedComment #13
ghost of drupal pastProgress report: the tokenizer form and the tokenizer replacement code has been lifted from
TokenizeAreaPluginBase, that would work but metatags runs fromhook_page_attachment()completely disjoint from the main controller workflow and$this->viewinMetatagDisplayExtender::getMetatagsis the$viewloaded bymetatag_views_metatag_route_entityand it is not built/executed/rendered and so the token replacement does not work. Researching ways to preserve the first row.Comment #14
ghost of drupal pastI pushed a solution which preserves the rendered metatags to git not sure whether it'll show up here as a patch, this workflow is still very new to me.
Comment #15
ghost of drupal pastThe patch has been moved to the
2952229-allow-views-metabranch so the top link works.To describe the changes: most of it is a copy of
TokenizeAreaPluginBase::tokenFormand an inlined copy ofTokenizeAreaPluginBase::tokenizeValue. The interesting part is where the tokenized metatags are saved in a static class property in order to avoid executing the Views twice, this is implemented bymetatag_views_views_post_rendercallingMetatagDisplayExtender::getMetatagswhich now tokenizes and saves the results. The real changes are best seen in the whitespace change suppressing diff.Comment #16
damienmckennaExcellent, thanks for sorting that out.
Could you please create a merge request with these changes so we can run them through the testbot? Thank you.
Comment #18
ghost of drupal pastUncached flow:
Cached flow
Solution: permanent the static cache. Done. Just run. What do we say to #2478393: hook_page_(attachments|attachments_alter|top|bottom)() should specify the right cacheability metadata? Not today.
Comment #19
damienmckennaThank you for that.
Now back to needing test coverage to make sure the functionality works as intended.
Comment #20
ghost of drupal pastD'oh. Had a stupid moment with the push last night but it's fixed.
Comment #21
ghost of drupal pastNote for reviewers:
updateViewstarts with$title_prefix = $this->randomMachineName();changing to this to$title_prefix = $this->randomString();fails the test indicatingfoo & bar {{ title }}will get escaped in so it reads foo & bar $node_title. I think this will stay so.Comment #22
ghost of drupal pastComment #23
ghost of drupal pastComment #24
ghost of drupal pastTo better understand the evolution of the caching of this patch:
Comment #25
dgwebcreative commentedTrying to test this but I'm not sure how it works. I've installed the forked module and have metatag_views enabled but I don't find the "Use replacement tokens from the first row" option in metatag settings or the views metatag settings. Views field tokens pasted in the metatag title are printed in raw token form {{ title }}.
Comment #26
ghost of drupal pastI am not sure how to help, maybe try to follow what the automated test does? We know that one works: creates a view with a page display row style titles, and then it sets the tokenize option to 1... it's weird you don't have that one. Maybe find me on slack some time between 2PM and 2AM Pacific Time and let's do a screenshare? My nick is chx.
Comment #27
joseph.olstad@Charlie ChX Negyesi,
Testing this patch now, I apply the patch then rebuild cache, then I see the new option "Use replacement tokens from the first row" that shows up at the bottom of the views metatag override dialog box
As soon as I checked this box my contextual filter value showed up as I expected.
I will run more tests, so far so good though, thanks very much!
Comment #28
joseph.olstadThe merge request includes tests and everything works as I would expect.
Thanks so much for adding this functionality!
Comment #30
damienmckennaCommitted. Thanks everyone for your work! I added an update script to clear the caches so the functionality would be available.
Comment #31
damienmckennaComment #32
nikathoneI did apply the patch at https://git.drupalcode.org/project/metatag/-/merge_requests/5.diff and started to get
followed by
If I removed the patch and re-import my views these two errors are gone.
Comment #34
acI got similar issues to #32 when using this merge request. When used on a view with facets, it had the effect of hiding all the facets, even when they should be visible.
Comment #35
stephen ollmanOn D9.3 and have recently updated to Metatag 8.x-1.18 and have just started to get this same error.
Notice: Trying to get property 'build_info' of non-object in Drupal\views\Plugin\views\cache\CachePluginBase->generateResultsKey() (line 199 of /home/XXXXX/public_html/core/modules/views/src/Plugin/views/cache/CachePluginBase.php)
Followed by:
Error: Call to a member function getCurrentPage() on null in Drupal\views\Plugin\views\cache\CachePluginBase->generateResultsKey() (line 220 of /home/XXXXX/public_html/core/modules/views/src/Plugin/views/cache/CachePluginBase.php)
No idea what has caused this but started happening post Metatag module upgrade.
Result is that Views do not display any preview result output.
Comment #36
oxy86 commentedSame problem here as Stephen Ollman. Two D9.3 websites (both migrated from D7) with Metatag 8.x-1.18. When the metatag_views submodule is enabled, the preview function in all views does not work. This is the error I get:
Comment #37
oxy86 commentedPlease note, that by downgrading to 1.6 solved the problem for me:
Now the preview in views works as before.
Comment #38
eugene bocharov commentedWrapping 'none' cache plugin, which is used by views in preview, cause errors.
I suggested patch at https://www.drupal.org/project/metatag/issues/3255547
Comment #39
baltazarz3 commented#37 worked for me. My watchdog logs table was not working due to an error on its ajax callback.
Thank you