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.
To alter a meta tag in hook_metatag_metatags_view_alter() is pretty cumbersome as I have to mess with the render array:
function MY_MODULE_metatag_metatags_view_alter(&$output, $instance, $options) {
$output['title']['#attached']['drupal_add_html_head'][0][0]['#value'] = 'My fancy title';
// Changing a page from "index" to "noindex" requires string manipulation...
$robots = &$output['robots']['#attached']['drupal_add_html_head'][0][0]['#value'];
$robots = str_replace(' index', ' noindex', $robots);
}
There should be a hook_metatag_metatags_alter() to allow altering the meta tags before they are put into the render array. This would be much simpler, just change some values:
function MY_MODULE_metatag_metatags_alter(&$metatags, $instance, $options) {
if ($instance == 'MY_INSTANCE') {
$metatags['title']['value'] = 'My fancy title';
$metatags['robots']['value']['noindex'] = 'noindex';
$metatags['robots']['value']['index'] = 0;
}
}
Comment | File | Size | Author |
---|---|---|---|
#2 | metatag-allow_alter_before_render_array-3037897-2.patch | 3.81 KB | Ronino |
|
Comments
Comment #2
Ronino CreditAttribution: Ronino as a volunteer commentedThis patch adds hook_metatag_metatags_alter().
Comment #3
Ronino CreditAttribution: Ronino as a volunteer commentedComment #4
DamienMcKennaThanks for putting this together.
Does metatag_views need to be updated too?
Comment #5
Ronino CreditAttribution: Ronino as a volunteer commentedI don't think so as metatag_views_page_alter() calls metatag_metatags_view() which calls the new hook ;-)
Comment #7
DamienMcKennaCommitted, with a few minor tweaks. Thanks!
Comment #8
DamienMcKennaComment #9
DamienMcKenna