You are here

function _piwik_get_hierarchy_titles in Piwik Web Analytics 8

Same name and namespace in other branches
  1. 6.2 piwik.module \_piwik_get_hierarchy_titles()
  2. 7.2 piwik.module \_piwik_get_hierarchy_titles()

Get the page titles trail for the current page.

Based on menu_get_active_breadcrumb().

Return value

array All page titles, including current page.

1 call to _piwik_get_hierarchy_titles()
piwik_page_attachments in ./piwik.module
Implements hook_page_attachments().

File

./piwik.module, line 682
Drupal Module: Piwik.

Code

function _piwik_get_hierarchy_titles() {
  $titles = [];
  $path = Url::fromRoute('<current>')
    ->toString();
  $config = \Drupal::config('system.site');
  $piwik_conf = \Drupal::config('piwik.settings');

  // No breadcrumb for the front page.
  if ($path === Url::fromUserInput($config
    ->get('page.front'))
    ->toString()) {
    return $titles;
  }

  // Load up the menu tree.
  // TODO: Check this is a sane approach.
  $menu_tree = \Drupal::menuTree();
  $menu_name = \Drupal::config('system.menu.main')
    ->get('id');

  // Build the typical default set of menu tree parameters.
  $parameters = $menu_tree
    ->getCurrentRouteMenuTreeParameters($menu_name);

  // Load the tree based on this set of parameters.
  $tree = $menu_tree
    ->load($menu_name, $parameters);

  // Transform the tree using the manipulators.
  $manipulators = [
    // Only show links that are accessible for the current user.
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    // Use the default sorting of menu links.
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
    // Fatten the menu.
    [
      'callable' => 'menu.default_tree_manipulators:flatten',
    ],
  ];
  $tree = $menu_tree
    ->transform($tree, $manipulators);
  if (!empty($tree)) {
    foreach ($tree as $menu_item) {

      // If the item is in the active trail and we don't have a front page link
      // when we have set to exclude home from the breadcrumbs then add the
      // title.
      if ($menu_item->inActiveTrail && !($piwik_conf
        ->get('page_title_hierarchy_exclude_home') && $menu_item->link
        ->getRouteName() == '<front>')) {
        $titles[] = $menu_item->link
          ->getTitle();
      }
    }
  }
  return $titles;
}