You are here

function piwik_preprocess_item_list__search_results in Piwik Web Analytics 8

Implements hook_preprocess_item_list__search_results().

Collects and adds the number of search results to the head.

File

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

Code

function piwik_preprocess_item_list__search_results(&$variables) {
  $config = \Drupal::config('piwik.settings');

  // Only run on search results list.
  if ($config
    ->get('track.site_search')) {

    // There is no search result $variable available that hold the number of
    // items found. The variable $variables['items'] has the current page items
    // only. But the pager item mumber can tell the number of search results.
    global $pager_total_items;
    $variables['#attached']['html_head'][] = [
      [
        '#tag' => 'script',
        '#value' => 'window.piwik_search_results = ' . intval($pager_total_items[0]) . ';',
        '#weight' => JS_LIBRARY - 1,
      ],
      'piwik_search_script',
    ];
  }
}