function schema_metatag_page_attachments_alter in Schema.org Metatag 8.2
Same name and namespace in other branches
- 8 schema_metatag.module \schema_metatag_page_attachments_alter()
Implements hook_page_attachments_alter().
Load all meta tags for this page, then separate out the structured data.
File
- ./
schema_metatag.module, line 30 - Contains schema_metatag.module.
Code
function schema_metatag_page_attachments_alter(array &$attachments) {
if (empty($attachments['#attached']['html_head'])) {
return;
}
// Collect tags added by Schema Metatag into structured data array.
$schema_metatag_manager = \Drupal::service('schema_metatag.schema_metatag_manager');
$items = $schema_metatag_manager
->parseJsonld($attachments['#attached']['html_head']);
// Turn the structured data array into JSON LD and add it to page head.
if (count($items) > 0) {
$jsonld = $schema_metatag_manager
->encodeJsonld($items);
if (!empty($jsonld)) {
$attachments['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => $jsonld,
'#attributes' => [
'type' => 'application/ld+json',
],
],
'schema_metatag',
];
}
}
}