function site_verify_page_attachments in Site verification 8
Implements hook_page_attachments().
Add a meta tag to the HEAD.
File
- ./
site_verify.module, line 53 - Used to demonstrate the possibilities of routing in Drupal 8.
Code
function site_verify_page_attachments(array &$page) {
if (\Drupal::service('path.matcher')
->isFrontPage()) {
$meta_tags = \Drupal::database()
->select('site_verify', 'site_verify')
->fields('site_verify', [
'svid',
'meta',
])
->condition('meta', '', '<>')
->execute()
->fetchAllKeyed();
foreach ($meta_tags as $svid => $meta_tag) {
preg_match('/name="(.*)" content/', $meta_tag, $name);
preg_match('/content="(.*)"/', $meta_tag, $content);
$data = [
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => [
'name' => $name[1],
'content' => $content[1],
],
];
$page['#attached']['html_head'][] = [
$data,
'site_verify:' . $svid,
];
}
}
}