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;
  }
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Ronino created an issue. See original summary.

Ronino’s picture

This patch adds hook_metatag_metatags_alter().

Ronino’s picture

Status: Active » Needs review
DamienMcKenna’s picture

Version: 7.x-1.25 » 7.x-1.x-dev

Thanks for putting this together.

Does metatag_views need to be updated too?

Ronino’s picture

Does metatag_views need to be updated too?

I don't think so as metatag_views_page_alter() calls metatag_metatags_view() which calls the new hook ;-)

  • DamienMcKenna committed 32ea2c0 on 7.x-1.x authored by Ronino
    Issue #3037897 by Ronino, DamienMcKenna: Allow raw meta tags to be...
DamienMcKenna’s picture

Status: Needs review » Fixed
Parent issue: » #2958474: Plan for Metatag 7.x-1.26

Committed, with a few minor tweaks. Thanks!

DamienMcKenna’s picture

Title: Allow raw meta tags to be modified prior to creating the render array » Allow raw meta tags to be modified prior to creating the render array (D7)
DamienMcKenna’s picture

Title: Allow raw meta tags to be modified prior to creating the render array (D7) » Allow raw meta tags to be modified prior to rendering (D7)

Status: Fixed » Closed (fixed)

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