Problem/Motivation

I have a custom set of routes that uses parameters to display data, but the data isn't an entity at all. I've written my own hook_metatags_alter() to load in the defaults for my routes, but the problem is I want to provide some custom data to the token replacement, but there's no way to alter it.

Proposed resolution

Allow hook_metatags_alter() to let modules alter or add token replacement data:

function mymodule_metatags_alter(array &$metatags, array &$context) {
  if (mypage_special_route()) {
    $context['data']['my-data'] = mypage_special_route_data();
  }
}

Remaining tasks

User interface changes

None

API changes

  • Additional context available to alter in hook_metatags_alter().
  • Method definition of \Drupal\metatag\MetatagManager::generateElements() and \Drupal\metatag\MetatagManager::generateRawElements() from taking an entity object, to taking an array of token data and a langcode. The preparation of the token data now happens upstream in metatag_get_tags_from_route(). Note that while both of these methods were public, they were not defined in MetatagManagerInterface. The generateRawElements() method even had an unused parameter that was removed.

Data model changes

None

Issue fork metatag-3307202

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid created an issue. See original summary.

Dave Reid’s picture

Issue summary: View changes

Dave Reid’s picture

Status: Active » Needs review
DamienMcKenna’s picture

Rerolled.

It also reverts one minor change that allows the entity passed into hook_metatags_alter() to be modified.

Status: Needs review » Needs work

The last submitted patch, 5: metatag-n3307202-5.patch, failed testing. View results

Dave Reid’s picture

Are you sure? As long as the function that implements hook_metatags_alter() has the parameter $context itself by reference, they can alter the object in the context just fine: https://3v4l.org/DMcIk. Objects are passed by reference by default. I believe the version in the MR code is correct.