How to change a metatag programaticly on drupal ?
Add meta tag from controller
Example: Add <meta http-equiv="refresh" content="30">
$data = [
'#tag' => 'meta',
'#attributes' => [
'http-equiv' => 'refresh',
'content' => '30',
],
];
$output['#attached']['html_head'][] = [$data, 'refresh'];
And also:
The metatag module provide an API (hook) to alter all meta tags : hook_metatag_metatags_view_alter
Example:
NOTE : This is not the drupal best practice.
/**
*
* Implement hook_metatag_metatags_view_alter().
* - Translate 'description' metatag of the home page.
* @param type $output
* @param type $instance
* @param type $options
*/
function mymodule_metatag_metatags_view_alter(&$output, $instance, $options) {
if ($instance == 'global:frontpage') {
// Use dpm or kint to get the field to change.
if (!empty($value = $output['description']['#attached']['drupal_add_html_head'][0][0]['#value'])) {
$output['description']['#attached']['drupal_add_html_head'][0][0]['#value'] = "The new value";
}
}
}
Comments