You are here

function _matomo_get_hierarchy_titles in Matomo Analytics 8

Same name and namespace in other branches
  1. 7.2 matomo.module \_matomo_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 _matomo_get_hierarchy_titles()
matomo_page_attachments in ./matomo.module
Implements hook_page_attachments().

File

./matomo.module, line 691
Drupal Module: Matomo.

Code

function _matomo_get_hierarchy_titles() {
  $titles = [];
  $path = Url::fromRoute('<current>')
    ->toString();
  $config = \Drupal::config('system.site');
  $matomo_conf = \Drupal::config('matomo.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 && !($matomo_conf
        ->get('page_title_hierarchy_exclude_home') && $menu_item->link
        ->getRouteName() == '<front>')) {
        $titles[] = $menu_item->link
          ->getTitle();
      }
    }
  }
  return $titles;
}