structured_data.module in Structured Data (JSON+LD Rich Snippets) 8
Same filename and directory in other branches
Contains module functions for Structured Data.
File
structured_data.moduleView source
<?php
/**
* @file
* Contains module functions for Structured Data.
*/
use Drupal\Core\Url;
use Drupal\structured_data\Core\Helper;
/**
* Implements hook_toolbar_alter().
*/
function structured_data_toolbar_alter(&$items) {
$user = \Drupal::currentUser();
if ($user
->hasPermission('manage page structured data json')) {
$page_meta = Helper::getCurrentPageMeta(TRUE);
$querystring = \Drupal::getContainer()
->get('request_stack')
->getCurrentRequest()
->getQueryString();
if (!empty($querystring)) {
$page_meta['url'] .= '?' . $querystring;
}
$page_meta['url'] = base64_encode($page_meta['url']);
$page_meta['url'] = str_replace('/', '|', $page_meta['url']);
if (empty($items['shortcuts']['tray']['shortcuts'])) {
$items['shortcuts']['tray']['shortcuts'] = [
'#theme' => 'links__toolbar_shortcuts',
'#links' => [],
'#attributes' => [
'class' => [
'toolbar-menu',
],
],
];
}
$items['shortcuts']['tray']['shortcuts']['#links'][] = [
'type' => 'link',
'title' => t('Page Json'),
'url' => Url::fromRoute('structured_data.page.json', [
'sd_route_name' => $page_meta['route_name'],
'sd_url' => $page_meta['url'],
'sd_bundle' => $page_meta['bundle'],
'sd_entity_id' => $page_meta['entity_id'],
]),
'attributes' => [
'title' => t('Page structured data json'),
'class' => [
'structured-data-page-json-link',
],
'data-url-template' => Url::fromRoute('structured_data.page.json', [
'sd_route_name' => $page_meta['route_name'],
'sd_url' => '--template--',
'sd_bundle' => $page_meta['bundle'],
'sd_entity_id' => $page_meta['entity_id'],
])
->toString(),
],
'cache' => [
'contexts' => [
'user',
'url',
],
],
];
}
}
/**
* Implements hook_page_attachments_alter().
*/
function structured_data_page_attachments_alter(array &$attachments) {
$page_meta = Helper::getCurrentPageMeta();
$obj = Helper::getPageJson($page_meta);
if (!empty($obj)) {
$tag = [
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [
'type' => 'application/ld+json',
],
'#value' => $obj->json,
];
$attachments['#attached']['html_head'][] = [
$tag,
'structured_data_json',
];
}
$user = \Drupal::currentUser();
if ($user
->hasPermission('manage page structured data json')) {
$attachments['#attached']['library'][] = 'structured_data/toolbar';
}
}
Functions
Name | Description |
---|---|
structured_data_page_attachments_alter | Implements hook_page_attachments_alter(). |
structured_data_toolbar_alter | Implements hook_toolbar_alter(). |