You are here

function siteimprove_toolbar in Siteimprove 8

Implements hook_toolbar().

Only show prepublish action for enabled taxonomies and content types.

2 string references to 'siteimprove_toolbar'
SettingsForm::buildForm in src/Form/SettingsForm.php
Form constructor.
_siteimprove_toolbar_basic in ./siteimprove.module
Basic toolbar array.

File

./siteimprove.module, line 17
Drupal module: Siteimprove Plugin.

Code

function siteimprove_toolbar() {
  $user = \Drupal::currentUser();
  if (!$user
    ->hasPermission('use siteimprove')) {
    return [];
  }
  $node = \Drupal::routeMatch()
    ->getParameter('node');
  $taxonomy = \Drupal::routeMatch()
    ->getParameter('taxonomy_term');
  $bundle = '';

  //In event node parameter is not a node object.
  if (is_string($node)) {
    $node = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->load($node);
  }

  // In event taxonomy parameter is not an Term object.
  if (is_string($taxonomy)) {
    $taxonomy = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->load($taxonomy);
  }
  $urls = \Drupal::service('siteimprove.utils')
    ->getEntityUrls($node ? $node : $taxonomy);
  $toolbar_items = [];
  $toolbar = [];

  // Get bundle from either node or taxonomy.
  if (is_object($node) && $node instanceof \Drupal\node\Entity\Node) {
    $bundle = $node
      ->bundle();
  }
  elseif (is_object($taxonomy) && $taxonomy instanceof \Drupal\taxonomy\Entity\Term) {
    $bundle = $taxonomy
      ->bundle();
  }

  /**
   * Prepublish toolbar item.
   */
  $prepublish_access = $user
    ->hasPermission('use siteimprove prepublish');
  $prepublish_available = FALSE;
  $config = \Drupal::service('config.factory')
    ->get('siteimprove.settings');
  $prepublish_enabled = $config
    ->get('prepublish_enabled');
  $enabled_content_types = $config
    ->get('enabled_content_types');
  $enabled_taxonomies = $config
    ->get('enabled_taxonomies');
  $enabled_route_names = [
    'entity.node.canonical',
    'entity.node.latest_version',
    'entity.taxonomy_term.canonical',
    'entity.taxonomy_term.latest_version',
  ];
  $current_route_name = \Drupal::routeMatch()
    ->getRouteName();

  // Prepublish should be available for an enabled taxonomy or content type.
  if ($prepublish_enabled && in_array($current_route_name, $enabled_route_names) && ($node && $enabled_content_types[$bundle] || $taxonomy && $enabled_taxonomies[$bundle])) {
    $prepublish_available = TRUE;
  }

  // Add prepublish action if enabled.
  if ($prepublish_access && $prepublish_available) {
    $toolbar_items['siteimprove_toolbar']['tray']['actions']['#items']['prepublish'] = [
      '#type' => 'link',
      '#title' => 'Prepublish check',
      '#url' => Url::fromUserInput('#'),
      '#attributes' => [
        'class' => [
          'siteimprove-contentcheck-button',
        ],
      ],
      '#wrapper_attributes' => [
        'class' => [
          'menu-item',
        ],
      ],
    ];
  }

  /**
   * Recheck toolbar item.
   */
  $recheck_access = $user
    ->hasPermission('use siteimprove');
  $enabled_route_names = [
    'entity.node.canonical',
    'entity.node.edit_form',
    'entity.taxonomy_term.canonical',
    'entity.taxonomy_term.edit_form',
  ];
  $current_route_name = \Drupal::routeMatch()
    ->getRouteName();
  if ($recheck_access && in_array($current_route_name, $enabled_route_names)) {
    $toolbar_items['siteimprove_toolbar']['tray']['actions']['#items']['recheck'] = [
      '#type' => 'link',
      '#title' => 'Recheck',
      '#url' => Url::fromUserInput('#'),
      '#attributes' => [
        'class' => [
          'siteimprove-recheck-button',
        ],
      ],
      '#wrapper_attributes' => [
        'class' => [
          'menu-item',
        ],
      ],
    ];
    $toolbar_items['siteimprove_toolbar']['tray']['#attached']['drupalSettings']['siteimprove']['recheck'] = \Drupal::service('siteimprove.utils')
      ->getSiteimproveSettings($urls, 'recheck', FALSE);
  }

  /**
   * Settings page toolbar item.
   */
  if ($user
    ->hasPermission('administer siteimprove')) {
    $toolbar_items['siteimprove_toolbar']['tray']['actions']['#items']['settings'] = [
      '#type' => 'link',
      '#title' => 'Configuration',
      '#url' => Url::fromRoute('siteimprove.settings_form'),
      '#wrapper_attributes' => [
        'class' => [
          'menu-item',
        ],
      ],
    ];
  }

  // Add Siteimprove toolbar items.
  if (!empty($toolbar_items)) {
    $toolbar_basic = _siteimprove_toolbar_basic();
    $toolbar = NestedArray::mergeDeep($toolbar_basic, $toolbar_items);
  }

  // Current entity or null.
  $current_entity = $node ? $node : ($taxonomy ? $taxonomy : null);

  // Add current viewed entity as cacheable dependency.
  $renderer = \Drupal::service('renderer');
  if ($current_entity) {
    $renderer
      ->addCacheableDependency($toolbar, $current_entity);
  }
  return $toolbar;
}