In Drupal 7, the Metatag module leveraged the Token API to generate tokens such as [node:metatag:title].

In Drupal 8, it seems this is not in place and only provides a single token called [node:field_metatags:value].
When calling the Token::replace() method on that token, it returns the serialized string from the database, which isn't very helpful.

Most of the time, what a developer would want from the token is the actual data, such as the value stored on title or description, but using tokens in the same way as in D7, such as Token::Replace('[node:metatag:title]');

Comments

joshua.boltz created an issue. See original summary.

steffenr’s picture

henrikakselsen’s picture

Noticed the same issue. When trying to use [node:field_meta_tags:value] I just get the serialized string:

a:4:{s:11:"description";s:28:"[node:field_meta_tags:value]";s:12:"og_site_name";s:11:"[site:name]";s:8:"og_title";s:12:"[node:title]";s:14:"og_description";s:14:"[node:summary]";}
kyuubi’s picture

Bumping into this as well.
Any workaround that can be used?

remkovdz’s picture

Any news on this?

dchaffin’s picture

Any update on this?

damienmckenna’s picture

Title: Accessing metatag tokens » Tokens to access individual meta tag values
Issue tags: -metatag, -token, -serialized

Nothing yet.

rwohleb’s picture

Status: Active » Needs review
StatusFileSize
new7.88 KB

An initial pass at adding token support. Inspiration taken from the token contrib module.

My particular use-case is using metatag tokens for title, description, etc and injecting that into schema_metatag tags. This way an editor can set a custom description on a node and it will be reflected in the structured data.

rwohleb’s picture

StatusFileSize
new7.87 KB

A cleaned up version of the patch.

ssankarsiva’s picture

Applied #9 patch, it works fine.

Phil Wolstenholme’s picture

I can't get [metatag:description] to work with #9, although [metatag:title] works.

I'm trying to set my OG and Twitter card title and description fields to use whatever my Basic metatag title and description is for each node. This approach works for the title but not for the description. I can reproduce it on SimplyTest.me as well as my current project.

damienmckenna’s picture

Status: Needs review » Needs work

Thanks for testing the patch, Phil.

Let's add some test coverage to itemize what this functionality is expected to actually do.

damienmckenna’s picture

Issue tags: +Needs tests
vitalyos’s picture

@Damien
Thank you for the patch

Same here.

Applied #9 and it doesn't work correctly

if I try to use [metatag:description] in another metatag field. As I see it in loop inside the between metatag_tokens() and generateTokenValues() in MetatagManager.php class.

danielveza’s picture

Confirming the issue in #14. Site goes down with the patch when you put a metatag token into a metatag field.

Looking now.

Fairly certain the issue is on this line

$processed_value = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->tokenService->replace($tag->value(), $token_replacements, ['langcode' => $langcode])));

danielveza’s picture

StatusFileSize
new8.74 KB
new11.97 KB

I've fixed the recursion issue outlined in comments 14 & 15.

My interdiffs gone weird for some reason but essentially I took @berdirs approach from #2955407: Static caching for token processing; iconv_substr() is super slow and added a processedTokensCache to the MetatagManager class.

Then in metatag.tokens.inc I've added a check if the processedTokensCache is empty. It the processedTokensCache has data in it then it's already been run so we can safely break out of the infinite loop.

I originally wrote the patch for this for a site on metatag 1.11, I've tried to retrofit it into the latest commit on 1.x. It *shouldn't* break, but I haven't tested it yet. It needs a proper run through. And test coverage, of course. But I'm out of time for today.

danielveza’s picture

Status: Needs work » Needs review

Marking needs review to check the tests pass.

I just clicked after uploading the patch that the MetatagManagerInterface will need the new function & property added.

damienmckenna’s picture

Status: Needs review » Needs work

Seems like the patch needs to be rerolled or something? The 8.x-1.x branch doesn't have an existing metatag.tokens.inc file so the patch isn't applying correctly.

danielveza’s picture

Ah. Yes. I rolled the path running git add --intent-to-add metatag.tokens.inc && git diff...

Seems that was the cause of my interdiff being weird as well. It hasn't marked metatag.tokens.inc as previously being /dev/null so it's failed. I won't have the chance today, but I'll fix it up tomorrow.

danielveza’s picture

Status: Needs work » Needs review
StatusFileSize
new8.75 KB

Lets try this one.

This is actually the patch I wrote for 1.11, but it applies to the latest dev as well.

danielveza’s picture

Status: Needs review » Needs work
StatusFileSize
new9.05 KB

Patch in #20 had a bug, so I'm uploading a new version here which works from my testing.. But to be honest I'm not happy enough with the performance of this patch to recommend including it.

If I get some more time on this I'll look into refining it further. But it's paused for now. I'm just uploading this patch so others can pick it up if it's a feature they need. :)

jeroent’s picture

Status: Needs work » Needs review
damienmckenna’s picture

Status: Needs review » Needs work

This is great work, thank you all.

If you don't mind, I'm putting it back to "needs work" so some test coverage can be added before we commit it, that way we can have a specific idea of what we're getting with the change.

jeroent’s picture

Status: Needs work » Needs review
StatusFileSize
new5.17 KB
new14.44 KB
new16.02 KB

While writing the test, I stumbled upon a couple of bugs.

Default configured metatags were not working, but only metatags that were added on e.g. node form.

The metatag for canonical-url was not working because of the dash instead of underscore.

The last submitted patch, 24: 2862747-24-test-only.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jeroent’s picture

Issue tags: -Needs tests
jeroent’s picture

StatusFileSize
new15.81 KB
new17.91 KB

I guess [current-page:metatag:title] makes more sense than [metatag:title]. Also it makes testing the tokens easier since we can use a lot of code of the token module.

+ I also added support for fields. e.g. [node:field_metatags:title] is now also working.

Status: Needs review » Needs work

The last submitted patch, 27: 2862747-27.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jeroent’s picture

StatusFileSize
new15.84 KB
new3.91 KB
jeroent’s picture

Status: Needs work » Needs review
jeroent’s picture

StatusFileSize
new15.89 KB
new307 bytes
new15.89 KB

damienmckenna’s picture

Status: Needs review » Fixed
Parent issue: » #3164682: Plan for Metatag 8.x-1.15

Excellent work, thank you all!

damienmckenna’s picture

Status: Fixed » Closed (fixed)

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

coufu’s picture

One of the earlier patches that allowed `[metatag:title]` and `[metatag:description]` worked perfectly.

However, the merged patch broke being able to pull values from manually entered metatag title and description to other fields (like og:title and og:description), even when using the new token format `[node:field_metatags:title]` etc.

damienmckenna’s picture

@Coufu: please open a new issue and we can look at it; we'll definitely need to extend the test coverage for additional use cases.