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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

djschoone’s picture

Component: Integration with other module » Views integration
Issue tags: +available token list
vlooivlerke’s picture

This 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.

bpleduc’s picture

Version: 7.x-1.0-beta7 » 7.x-1.x-dev
FileSize
3.89 KB

I 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.

DamienMcKenna’s picture

Status: Active » Needs review
Issue tags: -available token list

@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.

cimo75’s picture

Working here, thanks for the patch!

yurg’s picture

Patch was tested, works for me, thank you!

kopeboy’s picture

Nice feature, can we include it in a stable release?

bjalford’s picture

Status: Needs review » Reviewed & tested by the community

Works for me too

vlooivlerke’s picture

yes please comment it

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Needs work

The patch needs a little tidying to match the Drupal coding standards, but it looks promising!

bpleduc’s picture

Status: Needs work » Needs review
FileSize
4.04 KB

Drupal Standards Coding edits.

DamienMcKenna’s picture

Status: Needs review » Needs work

Thanks. 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.

bpleduc’s picture

Status: Needs work » Needs review
FileSize
4.07 KB

Hopefully I have them all now :)

Fixed "foreach" spacing before parenthesis and made comments sentences with wrapping at character 80 :)

DamienMcKenna’s picture

I made a few additional small tweaks.

lquessenberry’s picture

I 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.

DamienMcKenna’s picture

@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.

davidthou’s picture

FileSize
3.1 KB

Thanks for the patch, rolling an updated patch for stable version (1.4).

lquessenberry’s picture

Thanks 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.

lquessenberry’s picture

The patch worked in #17. Thanks fellas.

tribsel’s picture

hi, 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.

pingwin4eg’s picture

From the patches it seems that you forgot about placeholders from contextual filters: !1, %1, etc. Is it so? Or maybe I am missing something.

DamienMcKenna’s picture

DamienMcKenna’s picture

I 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?

bpleduc’s picture

@DamienMcKenna - Yes that is as expected.

DamienMcKenna’s picture

So 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.

bpleduc’s picture

@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?

DamienMcKenna’s picture

I'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.

DamienMcKenna’s picture

Title: Allow views' metatags to "Use replacement tokens from the first row" » Allow Views meta tags to "Use replacement tokens from the first row"
Issue summary: View changes

Clarifying 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.

gunwald’s picture

I tested the patch in #17 but I get a php error:

    Notice: Undefined index: und in metatag_views_page_alter() (line 96 of .../sites/all/modules/metatag/metatag_views/metatag_views.module).
    Warning: Invalid argument supplied for foreach() in metatag_views_page_alter() (line 96 of ...sites/all/modules/metatag/metatag_views/metatag_views.module).
drunken monkey’s picture

Status: Needs work » Needs review
FileSize
5.32 KB

Another 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's option_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?

herd45’s picture

I've tested #30 on the latest dev and it works as expected.

drunken monkey’s picture

Status: Needs review » Needs work

After 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?

DamienMcKenna’s picture

Is 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?

drunken monkey’s picture

At 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.

DamienMcKenna’s picture

Issue tags: +Needs tests

Ok, you know what that means then :)

drunken monkey’s picture

I 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.

Ok, you know what that means then :)

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.

DamienMcKenna’s picture

@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.

DamienMcKenna’s picture

FYI I have some tests for Metatag:Views as part of #2564483: Cannot translate metatags after last update anymore.

Status: Needs review » Needs work

The last submitted patch, 36: 2143979-36--views_result_replacement_tokens.patch, failed testing.

The last submitted patch, 36: 2143979-36--views_result_replacement_tokens.patch, failed testing.

akoepke’s picture

I 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.

thirdender’s picture

This 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 to metatag_translate_metatags was changed, and the comment // Update the i18n strings. was moved.

g33kg1rl’s picture

I 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.

Nchase’s picture

Patch #42 works for me.

philsward’s picture

Hope 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?

ruha’s picture

I can confirm that #42 is working well. Thanks for the hard work.

unstatu’s picture

Status: Needs work » Reviewed & tested by the community

I confirm #42 also worked for me.

There are 5 people including me that applied the patch successfully. I'll change the status to RTBC.

  • DamienMcKenna committed 614e2b7 on 7.x-1.x authored by bpleduc
    Issue #2143979 by bpleduc, drunken monkey, DamienMcKenna, thirdender,...
DamienMcKenna’s picture

Status: Reviewed & tested by the community » Fixed
Parent issue: » #2891956: Plan for Metatag 7.x-1.23 release

Committed. 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

jasonschweb’s picture

I'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.

jasonschweb’s picture

So far while debugging, I've discovered that tokenize_value is returning an empty array due to row_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.

DamienMcKenna’s picture

@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.

jasonschweb’s picture

@DamienMcKenna: No problem, created new issue here: https://www.drupal.org/node/2914921

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Alex Bukach’s picture

Is it implemented for D8?

DamienMcKenna’s picture

hossein425’s picture

Version: 7.x-1.x-dev » 7.x-1.25
Category: Feature request » Bug report
Issue tags: -Needs tests

As 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.

akoepke’s picture

Yep, 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.

DamienMcKenna’s picture

Version: 7.x-1.25 » 7.x-1.x-dev
Category: Bug report » Feature request

Please open a new issue for remaining problems with this functionality.