Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#5 | metatag-n3307202-5.patch | 8.16 KB | DamienMcKenna |
Issue fork metatag-3307202
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:
Comments
Comment #2
Dave ReidComment #4
Dave ReidComment #5
DamienMcKennaRerolled.
It also reverts one minor change that allows the entity passed into hook_metatags_alter() to be modified.
Comment #7
Dave ReidAre 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.