The meta tags module sets default meta tags that are rendering. However, I have a page with a panel and I am trying to override the default meta tags. I went into the panel and replaced the default meta tag tokens with custom text, but nothing changes. Any thoughts on why this might be happening? I'm using Drupal 7, and I'm hoping that we can do this through the admin UI.
2 Answers
In Drupal 8, there is a [module-name].module file inside your module. You can use "Hook attachments alter".
Code to alter your meta tags -
// For adding description in meta tag .
function hook_page_attachments_alter(array &$attachments) {
$attachments['#attached']['html_head'][] = array(
array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'description',
'content' => 'Any thing in meta description',
),
),
'description',
);
// For adding title in meta tag.
$attachments['#attached']['html_head'][] = array(
array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'title',
'content' => 'Any thing in meta title you want',
),
),
'title',
);
// For adding noindex in meta tag-robots .
$attachments['#attached']['html_head'][] = array(
array(
'#tag' => 'meta',
'#attributes' => array(
'rel' => 'robots',
'href' => 'noindex',
),
),
'noindex',
);
You can add more meta tags for different pages and can validate them too.
I have not worked much on drupal 7 but i found something called hook_html_head_alter in 7.
Code for it as -
function hook_html_head_alter(&$head_elements) {
foreach ($head_elements as $key => $element) {
if (isset($element['#attributes']['rel']) && $element['#attributes']['rel'] == 'canonical') {
// I want a custom canonical URL.
$head_elements[$key]['#attributes']['href'] = mymodule_canonical_url();
}
}
}
Note -
$head_elements: An array of renderable elements. Generally the values of the #attributes array will be the most likely target for changes.