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.
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.
Comment | File | Size | Author |
---|---|---|---|
#42 | 2143979-41--views_result_replacement_tokens.patch | 6.6 KB | thirdender |
| |||
#36 | 2143979-36--views_result_replacement_tokens.patch | 6.34 KB | drunken monkey |
Comments
Comment #1
djschoone CreditAttribution: djschoone commentedComment #2
vlooivlerke CreditAttribution: vlooivlerke commentedThis will be a valuable add on for the meta module as there is no dynamic way to create meta data for a view at this stage.
I have a city view that shows cities via an argument. imagine now each city page could have its own meta title and description populated by the first row of views tokens
Holding thumbs some super genius drupal developer needs this too.
Comment #3
bpleduc CreditAttribution: bpleduc commentedI have made a fix to allow Pattern Replacement in the Metatag Views module. You are now able to use any field in your Metatags. Support for nested tags is also included.
Comment #4
DamienMcKenna@bpleduc: Thanks for the patch. Please don't forget to change the issue status to "needs review" when you upload a patch, that'll test it a little and notify me (and others) that there's a patch to review.
Comment #5
cimo75 CreditAttribution: cimo75 commentedWorking here, thanks for the patch!
Comment #6
yurg CreditAttribution: yurg commentedPatch was tested, works for me, thank you!
Comment #7
kopeboy CreditAttribution: kopeboy commentedNice feature, can we include it in a stable release?
Comment #8
bjalford CreditAttribution: bjalford commentedWorks for me too
Comment #9
vlooivlerke CreditAttribution: vlooivlerke commentedyes please comment it
Comment #10
DamienMcKennaThe patch needs a little tidying to match the Drupal coding standards, but it looks promising!
Comment #11
bpleduc CreditAttribution: bpleduc commentedDrupal Standards Coding edits.
Comment #12
DamienMcKennaThanks. This still needs some further work to meet the code standards, though, e.g. comments must be complete sentences and wrap at character 80, add a space between "foreach" and the parenthesis, etc.
Comment #13
bpleduc CreditAttribution: bpleduc commentedHopefully I have them all now :)
Fixed "foreach" spacing before parenthesis and made comments sentences with wrapping at character 80 :)
Comment #14
DamienMcKennaI made a few additional small tweaks.
Comment #15
lquessenberry CreditAttribution: lquessenberry commentedI am going in circles here. How do you actually patch this? I have tried everything I can try to patch this thing and it keeps asking me which file I want to patch.
Comment #16
DamienMcKenna@lquessenberry: You need to run the "patch" command from the metatag directory, not from your site's root directory - please see Drupal's documentation for further details.
Comment #17
davidthou CreditAttribution: davidthou commentedThanks for the patch, rolling an updated patch for stable version (1.4).
Comment #18
lquessenberry CreditAttribution: lquessenberry commentedThanks for the response. I did that at first and I think I may have had issues with the patch I was using. Thanks for the help. Trying #17 now.
Comment #19
lquessenberry CreditAttribution: lquessenberry commentedThe patch worked in #17. Thanks fellas.
Comment #20
tribsel CreditAttribution: tribsel commentedhi, i used patch in #17, but nothing happened. I get a same list of tokens as before. Id like to use either metatags from taxonomy term /title, description/ or from view fields. View's description is for administration pages and users, not to be added to page.
Comment #21
pingwin4egFrom the patches it seems that you forgot about placeholders from contextual filters: !1, %1, etc. Is it so? Or maybe I am missing something.
Comment #22
DamienMcKennaComment #23
DamienMcKennaI tested it out, but it didn't use the values from the first row, it used them from the last row. Is this as expected?
Comment #24
bpleduc CreditAttribution: bpleduc commented@DamienMcKenna - Yes that is as expected.
Comment #25
DamienMcKennaSo is that really the "first row"? :) Not trying to nitpick, it's just that the output seemed contrary to what the UI indicates would happen. Other than this quandary, I'm happy with the patch, I just don't want to mislead people.
Comment #26
bpleduc CreditAttribution: bpleduc commented@DamienMcKenna - I'm a little confused about this. The patch does not add the checkbox as suggested originally. It just allows you to use your views fields as replacement tokens in Metatags.
Where are you seeing the UI mention that the first row is being used?
Comment #27
DamienMcKennaI've moved this patch into a separate issue focus on just adding the tokens for the first values: #2396693: Add Token support to insert values from the Views results
Lets re-focus this on the original request, see if it'd be possible.
Comment #28
DamienMcKennaClarifying that this is for Views' built-in replacement tokens, not the Tokens integration which is in #2396693: Add Token support to insert values from the Views results.
Comment #29
gunwald CreditAttribution: gunwald commentedI tested the patch in #17 but I get a php error:
Comment #30
drunken monkeyAnother attempt: is this about what you had in mind here?
I'm not too familiar with the Metatag module, unfortunately, but it seems to work alright. The code comes more or less directly from
views_handler_area_text
.One thing I'm wondering about, though, is the
options_definition()
method: I also added the new option there, but it doesn't seem to have any effect? Even when correcting the typo (it'soption_definition()
normally in Views) and the method actually does override the parent method, it doesn't seem to work for display extenders. So probably we should just remove that method?Comment #31
herd45 CreditAttribution: herd45 as a volunteer commentedI've tested #30 on the latest dev and it works as expected.
Comment #32
drunken monkeyAfter a bit of testing it turns out that this runs into problems when Views output caching is used, since the field tokens will then not be available.
Does anyone have a good suggestion to solve that problem? Should we try to somehow just trigger rendering of the first row before calling
tokenize_value()
? Or implement the functionality ourselves as a fallback? Or, maybe, we could try to add our own information to the cache and restore it from there, getting additional cachability as a bonus – could that work?Comment #33
DamienMcKennaIs the problem that the results don't populate at all, or that on the second page request the tokens are empty because they were cached on the first page load?
Comment #34
drunken monkeyAt the second page request (and all subsequent ones served from cache), the
<title>
content will be something like "[project_name] issues". At the first, uncached page request, everything works fine.Comment #35
DamienMcKennaOk, you know what that means then :)
Comment #36
drunken monkeyI managed to fix it quite easily, by just triggering manual rendering of the first restults row, if necessary. I also played around with adding the Metatags information to the Views output cache, but I couldn't get that to work – the Views cache plugin just isn't flexible enough for such special use cases.
It seems to me the Metatag Views module is completely untested, so I don't think it should be part of this issue to change that.
Comment #37
DamienMcKenna@drunkey monkey: Maybe there could be some (optional?) caching on the Metatag output at that point? Otherwise it may have to live on as a Known Issue.
Comment #38
DamienMcKennaFYI I have some tests for Metatag:Views as part of #2564483: Cannot translate metatags after last update anymore.
Comment #41
akoepke CreditAttribution: akoepke commentedI have just been testing this patch and noticed one bug. When there are results the patch works fine but if the View has no results then the tokens are left in the metatags. So the title of the page becomes "[title]".
Detecting that there are no results isn't that difficult but we need to have some way of entering some default values to use in this case. Really don't want to duplicate the entire Metatag form (very long!) for a set of default values.
Comment #42
thirdender CreditAttribution: thirdender commentedThis patch was very important to a project I'm working on right now. Basically, it's a site to track lost pets, and one of the critical features is the ability to share the pet's page on social media. I set up a View page to publicly display the pet's information, but the only way to get the pet photos into the meta tags is using this patch.
I wasn't able to apply the patch directly to the 7.x-1.x branch on git. I rerolled the patch against the latest commit on git, but I had to change a few things outside of the scope of the patch to get it to apply. Off the top of my head,
check_plain
was added on line 17, the call tometatag_translate_metatags
was changed, and the comment// Update the i18n strings.
was moved.Comment #43
g33kg1rl CreditAttribution: g33kg1rl commentedI tested the patch from #42 and it worked for me. :) The only issue I ran into was when I tried to create a backup I received an error about the cache_token from mysql being too large.
Comment #44
Nchase CreditAttribution: Nchase as a volunteer commentedPatch #42 works for me.
Comment #45
philsward CreditAttribution: philsward commentedHope to see this committed soon... I too would like to add images to a view and while I haven't tried the patch yet, totally expected to be able to use the tokens from within the view to do it.
Any timeline on incorporation?
Comment #46
ruha CreditAttribution: ruha commentedI can confirm that #42 is working well. Thanks for the hard work.
Comment #47
unstatu CreditAttribution: unstatu as a volunteer commentedI confirm #42 also worked for me.
There are 5 people including me that applied the patch successfully. I'll change the status to RTBC.
Comment #49
DamienMcKennaCommitted. Thanks everyone. FYI I've moved the test coverage into a separate issue: #2912483: Add test coverage for Metatag:Views "Use replacement tokens from the first row" feature
Comment #50
jasonschweb CreditAttribution: jasonschweb commentedI'm still having trouble with this when cache is enabled on a search_api view. Meta Description is present when the content is first viewed, but subsequent requests do not have it.
Tried enabled metatag cache, which did not solve the problem. Also disabled authcache - the only other cache we are using.
Comment #51
jasonschweb CreditAttribution: jasonschweb commentedSo far while debugging, I've discovered that
tokenize_value
is returning an empty array due torow_tokens
missing all the values. It looks like the field I'm using for metatag is also missing from$view->result
within_metatag_views_render_first_row
.Comment #52
DamienMcKenna@jasonschweb: I'm sorry you're having problems with this. Please open a new issue and we'll see what we can do to help fix the problem.
Comment #53
jasonschweb CreditAttribution: jasonschweb commented@DamienMcKenna: No problem, created new issue here: https://www.drupal.org/node/2914921
Comment #55
Alex Bukach CreditAttribution: Alex Bukach commentedIs it implemented for D8?
Comment #56
DamienMcKenna@Alex Bukach: No, so I opened #2952229: Allow Views meta tags to "Use replacement tokens from the first row" (D8).
Comment #57
hossein425 CreditAttribution: hossein425 commentedAs reported in #41 by @akoepke, when no results, values of "NO RESULTS BEHAVIOR" should be used if there is a single "Global: View area" and if there is more than one "Global: View area" or other cases, there should be "Global: Meta tags", to avoid public exposure of not replaced tags.
Comment #58
akoepke CreditAttribution: akoepke commentedYep, 2 years later and this issue still persists. Replacement tokens from the first row in Metatag works well until there are no results and then we end up with un-replaced tokens in the tags instead.
Comment #59
DamienMcKennaPlease open a new issue for remaining problems with this functionality.