Problem/Motivation

When I insert the [node:field_metatags:title] or [current-page:metatag:title] into the opengraph title metatag field, it uses the page title instead of the metatag title. We used this in D7 and it worked fine. This allows us to chain together the title and description overrides. But for D8 it's not working.

Steps to reproduce

  1. Enable the metatag opengraph module
  2. Navigate to the 'Manage Field' interface for a page content type (/admin/structure/types/manage/page/fields)
  3. Add a new field of type 'Meta Tags' with the machine name 'field_metatags'.
  4. Create a new page with the title "mypage" (/node/add/page)
  5. For Metatags > Basic Tags, set the page title to be "asdf"
  6. For Metatags > Opengraph, set the title to be "[node:field_metatags:title]"
  7. Save the new page
  8. In the page source, notice that the <title> is "asdf", but the og:title is "mypage | sitename". I expect og:title to also be "asdf".

Proposed resolution

As described in #8, this patch switches the order of metatag_get_default_tags($entity) with $metatag_manager->tagsFromEntity($entity). This ensures that entity-specific tokens are not overridden by default metatags.

Comments

bwaindwain created an issue. See original summary.

bwaindwain’s picture

Title: Problem with metatag tokens in the metatag fields » Problem with metatag tokens in metatag fields
Issue summary: View changes
Related issues: +#2827769: Get metatag values as tokens
bwaindwain’s picture

This bug seems to only affect title and description.

If I type "abc" in the abstract field and then put [node:field_metatags:abstract] in keywords, the keywords meta is "abc".

mortim07’s picture

I can't reproduce this on the dev branch or 1.15.

damienmckenna’s picture

Title: Problem with metatag tokens in metatag fields » "title" token doesn't work as expected

I wonder if it's a problem specific to the page title, because there is additional logic tied to how it works?

bwaindwain’s picture

If I set Opengraph title to [node:field_metatags:value] then I get

<meta property="og:title" content="a:2:{s:5:"title";s:4:"asdf";s:8:"og_title";s:27:"[node:field_metatags:value]";}">

which shows that "asdf" (the desired value) is in there somewhere (?!)

bwaindwain’s picture

It seems to have something to do with the metatag defaults. If I clear the Global and Content defaults, then the [current-page:metatag:title] token works as expected.

But, of course, then new nodes don't get the default tokens.

eugene bocharov’s picture

Version: 8.x-1.15 » 8.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new1.06 KB

It seems like it isn't specific to the title. I have same problem for [current-page:metatag:description] and [term:field_metatags:description]

I look at this code

      $tags = metatag_get_default_tags($entity);
      if ($entity instanceof ContentEntityInterface) {
        // If content entity does not have an ID the page is likely an "Add"
        // page, so skip processing for entity which has not been created yet.
        if (!$entity->id()) {
          return NULL;
        }

        $tags += $metatag_manager->tagsFromEntity($entity);
      }

May be we should swap metatag_get_default_tags($entity) with $metatag_manager->tagsFromEntity($entity) block? Because default metatags take precedence over entity's ones now. So if default tag is presented, token get it instead of enity specific.

eugene bocharov’s picture

I added the tests to the previous patch. I haven't much exerience with writing tests yet, so if some more experienced person could take a look at this, it would be great.

The last submitted patch, 9: metatag-token-doesnt-work-as-expected-3189429-9-TEST-FAIL.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

eugene bocharov’s picture

StatusFileSize
new2.03 KB

Oh, indentation problem, sorry. Fixed

njim’s picture

Issue summary: View changes

Updated the steps to reproduce to be useful from a fresh Drupal install.

njim’s picture

Issue summary: View changes
njim’s picture

Status: Needs review » Reviewed & tested by the community

The patch applied correctly and I can confirm that the issue is resolved. Setting a value in the '' tag will trickle down as a token when used in an open-graph title metatag. I tested via Tugboat and updated the test steps within this ticket description.

This change makes sense to me. I would expect overrides to metatags to cascade through the 'general' section into more particular sections like open-graph metatags. But this change should be appropriately documented to inform users of possible regressions which they may not expect.

Code looks good and follows Drupal standards.

damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed
Parent issue: » #3203686: Plan for Metatag 8.x-1.17

Good work, thank you!

Status: Fixed » Closed (fixed)

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