Tokens not replaced when placing a view via TVI

Created on 20 September 2023, over 1 year ago

Problem/Motivation

When using tokens to valorise metatags inside a views embed via TVI (Taxonomy Views Integrator), they aren't replaced\correctly filled.

Steps to reproduce

  • Install metatag, metatag_views, token and tvi.
  • Configure the taxonomy_term view or another new with some metatag, e.g. "canonical url", using tokens
  • Go to a vocabulary settings and override the default view with TVI

I could get tokens work while in other part of the view - e.g. inside a textarea in the footer - while not in the metatag settings.
Therefore, I choose to open this issue on this module queue, but I couldn't identify code-wise where the problem lies, so it could be actually somewhere else.

🐛 Bug report
Status

Active

Version

2.0

Component

Views integration

Created by

🇮🇹Italy Giuseppe87

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Giuseppe87
  • 🇺🇸United States DamienMcKenna NH, USA

    Are you setting the tokens inside the view or on the Metatag defaults?

  • 🇮🇹Italy Giuseppe87

    Inside the view

  • 🇺🇸United States DamienMcKenna NH, USA

    And what tokens did you use?

  • 🇮🇹Italy Giuseppe87

    On a custom view of content with a page display I tried to place either [site:mail] or [current-page:url] as page title or canonical url and work.

    But I re-state my initial comment: the metatag on the taxonomy_term embed via TVI don't appear at all, it's not just if using tokens.

    I've tried multiple metatag to test: page title, canonical url, Geographical position, robots noindex or nofollow, Author, Refresh.
    None of them seems to appears, no matter if I place plain text or one of two those tokens.

  • 🇺🇸United States DamienMcKenna NH, USA

    I'm still trying to work out the architecture you have created.

    Is the path of the page you're loading the entity path of the taxonomy term (taxonomy/term/TID), or a different path?

    Are the values you're trying to see on the page stored in the term entity's override field, or term defaults?

    If you run metatag_generate_entity_all_tags($term) with the appropriate term (try using "drush php"), do you get the values you expect?

  • 🇮🇹Italy Giuseppe87

    I'll try to use some screenshot:
    This is my taxonomy term view. It's a duplicate of the default one, using it or the default gives the same results

    This is its metatag settings - also canonical url and nofollow and noindex are set, just not in the screenshot.
    The textarea in the footer has the following text: "Footer with mail token:[site:mail]"

    Those are the TVI settings on the vocabulary page:

    This is the front of /taxonomy/term/1, just to show that the token in the footer is correctly replaced.

    This is the source page of /taxonomy/term/1: the metatags set in the view are not present

    Just as counter-test, I've set up a view of articles, with a display page and the same metatag settings.
    In that page they appear correctly:

    This is the result of running

    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load(1);
    metatag_generate_entity_all_tags($term);

    via Drush or Devel.

     array:2 [▼
      "title" => array:2 [▼
        "#tag" => "meta"
        "#attributes" => array:2 [▼
          "name" => "title"
          "content" => "tag 1 | D10"
        ]
      ]
      "canonical_url" => array:2 [▼
        "#tag" => "link"
        "#attributes" => array:2 [▼
          "rel" => "canonical"
          "href" => "https://d10site.ddev.site/taxonomy/term/1"
        ]
      ]
    ]
    

    In this case the test is not clear to me. The extra metatag are set inside the view, would this function consider them took?

  • 🇺🇸United States DamienMcKenna NH, USA

    Given that you are loading the entity's meta tags, I wonder if the meta tags provided by the entity are overriding what is defined for the view?

  • 🇮🇹Italy Giuseppe87

    I could have found the cause, inside metatag_views.module:

    /**
     * Implements hook_metatags_alter().
     */
    function metatag_views_metatags_alter(array &$metatags, array &$context) {
      if (!$context['entity'] instanceof ViewEntityInterface) {
        return;
      }
    
    [...]
    

    In case of the taxonomy_term view, the condition is true and thus the alter function doesn't do anything.

    Tracing a bit backwards, metatag_page_attachments() calls metatag_get_tags_from_route which in turn calls metatag_get_route_entity(), the $entity passed in the hook.

    There's is a lot of ad hoc logic to identify the right $entity depending on the different route names.
    However, from a quick glance, TVI does the replacement inside /src/Enhancer/RouteEnhancer.php
    where changes only the $defaults about the display.

    Therefore I'd say there's a "mismatch" between the $route_object and the actual display.

    At this point I believe there are at least two options:

    1. Opening a issue on TVI, because its approach causes this problem. However I don't know if there could be a different solution or a way to improve it.
    2. This issue becomes a feature request: hopefully, a metatag submodule could take care of integrating with TVI.
  • 🇺🇸United States dillix

    @giuseppe87 Did you fix that issue? I have similar problem with view for categories taxonomy for commerce products.

  • 🇮🇹Italy Giuseppe87

    It's long time and on a project I am not working anymore, but I think I used this workaround

    function my_module_views_metatags_alter(array &$metatags, array &$context) {
       /** @var EntityInterface $entity */
        $entity = $context['entity'];
        $taxonomy_bundles_to_tag = [
          'articles',
        ];
        if ($entity->getEntityTypeId() === 'taxonomy_term' && in_array($entity->bundle(), $taxonomy_bundles_to_tag, TRUE)) {
          $metatags['canonical_url'] = 'your logic';
      }
    }
    
    
  • 🇺🇸United States dillix

    Yea, I've made same kind workaround, but with hook_metatags_alter()

Production build 0.71.5 2024