From 737d9680f7b878d9bf374e7879f1d6db78c786a5 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Tue, 5 Jun 2018 20:27:58 +0300
Subject: [PATCH] Make Piwik helper more robust. (#1191)

- Check for current view in Piwik helper before trying to access it.
- Try also the current view for record driver in Piwik helper.
---
 .../src/VuFind/View/Helper/Root/Piwik.php     | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
index b0594a005e3..7ce3270573a 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
@@ -4,7 +4,7 @@
  *
  * PHP version 7
  *
- * Copyright (C) The National Library of Finland 2014-2016.
+ * Copyright (C) The National Library of Finland 2014-2018.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2,
@@ -257,10 +257,11 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
     protected function getSearchResults()
     {
         $viewModel = $this->getView()->plugin('view_model');
-        if ('layout/lightbox' === $viewModel->getCurrent()->getTemplate()) {
+        $current = $viewModel->getCurrent();
+        if (null === $current || 'layout/lightbox' === $current->getTemplate()) {
             return null;
         }
-        $children = $viewModel->getCurrent()->getChildren();
+        $children = $current->getChildren();
         if (isset($children[0])) {
             $template = $children[0]->getTemplate();
             if (!strstr($template, '/home') && !strstr($template, 'facet-list')) {
@@ -304,8 +305,16 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
      */
     protected function getRecordDriver()
     {
-        $viewModel = $this->getView()->plugin('view_model');
-        $children = $viewModel->getCurrent()->getChildren();
+        $view = $this->getView();
+        $viewModel = $view->plugin('view_model');
+        $current = $viewModel->getCurrent();
+        if (null === $current) {
+            $driver = $view->vars('driver');
+            if (is_a($driver, 'VuFind\RecordDriver\AbstractBase')) {
+                return $driver;
+            }
+        }
+        $children = $current->getChildren();
         if (isset($children[0])) {
             $driver = $children[0]->getVariable('driver');
             if (is_a($driver, 'VuFind\RecordDriver\AbstractBase')) {
-- 
GitLab