You are here

function structured_data_toolbar_alter in Structured Data (JSON+LD Rich Snippets) 8

Implements hook_toolbar_alter().

File

./structured_data.module, line 14
Contains module functions for Structured Data.

Code

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',
        ],
      ],
    ];
  }
}