Your software
My Mautic version is: 4.4.7
My PHP version is: 7.4
My Database type and version is: MariaDb
Your problem
My problem is:
Global search generating error when search query matches landing pages name.
These errors are showing in the log:
mautic.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalThrowableError: "Call to a member function getId() on array" at C:\xampp\htdocs\mautic\app\bundles\PageBundle\Views\SubscribedEvents\Search\global.html.php line 17 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function getId() on array at C:\\xampp\\htdocs\\mautic\\app\\bundles\\PageBundle\\Views\\SubscribedEvents\\Search\\global.html.php:17)"}
Steps I have tried to fix the problem:
I have found the app\bundles\PageBundle\Views\SubscribedEvents\Search\global.html.php
is receiving the array instead of object and when I change code in app\bundles\PageBundle\EventListener\SearchSubscriber.php
file it fixes the issue but my concern is why it creating array instead of object.
Below is the original mautic code
public function onGlobalSearch(MauticEvents\GlobalSearchEvent $event)
{
$str = $event->getSearchString();
if (empty($str)) {
return;
}
$filter = ['string' => $str, 'force' => []];
$permissions = $this->security->isGranted(
['page:pages:viewown', 'page:pages:viewother'],
'RETURN_ARRAY'
);
if ($permissions['page:pages:viewown'] || $permissions['page:pages:viewother']) {
if (!$permissions['page:pages:viewother']) {
$filter['force'][] = [
'column' => 'IDENTITY(p.createdBy)',
'expr' => 'eq',
'value' => $this->userHelper->getUser()->getId(),
];
}
$pages = $this->pageModel->getEntities(
[
'limit' => 5,
'filter' => $filter,
]);
if (count($pages) > 0) {
$pageResults = [];
foreach ($pages as $page) {
$pageResults[] = $this->templating->getTemplating()->renderResponse(
'MauticPageBundle:SubscribedEvents\Search:global.html.php',
['page' => $page]
)->getContent();
}
if (count($pages) > 5) {
$pageResults[] = $this->templating->getTemplating()->renderResponse(
'MauticPageBundle:SubscribedEvents\Search:global.html.php',
[
'showMore' => true,
'searchString' => $str,
'remaining' => (count($pages) - 5),
]
)->getContent();
}
$pageResults['count'] = count($pages);
$event->addResults('mautic.page.pages', $pageResults);
}
}
}
and below is the code with my changes which fixes the issue.
public function onGlobalSearch(MauticEvents\GlobalSearchEvent $event)
{
$str = $event->getSearchString();
if (empty($str)) {
return;
}
$filter = ['string' => $str, 'force' => []];
$permissions = $this->security->isGranted(
['page:pages:viewown', 'page:pages:viewother'],
'RETURN_ARRAY'
);
if ($permissions['page:pages:viewown'] || $permissions['page:pages:viewother']) {
if (!$permissions['page:pages:viewother']) {
$filter['force'][] = [
'column' => 'IDENTITY(p.createdBy)',
'expr' => 'eq',
'value' => $this->userHelper->getUser()->getId(),
];
}
$pages = $this->pageModel->getEntities(
[
'limit' => 5,
'filter' => $filter,
]);
if (count($pages) > 0) {
$pageResults = [];
foreach ($pages as $page) {
/** my changes ***/
if(is_array($page)){
$page= $page[0];
}
$pageResults[] = $this->templating->getTemplating()->renderResponse(
'MauticPageBundle:SubscribedEvents\Search:global.html.php',
['page' => $page]
)->getContent();
}
if (count($pages) > 5) {
$pageResults[] = $this->templating->getTemplating()->renderResponse(
'MauticPageBundle:SubscribedEvents\Search:global.html.php',
[
'showMore' => true,
'searchString' => $str,
'remaining' => (count($pages) - 5),
]
)->getContent();
}
$pageResults['count'] = count($pages);
$event->addResults('mautic.page.pages', $pageResults);
}
}
}
so you can see I am checking if the $page is array it should get the 0 index and assign it to $page which fixes the issue.
To generate the issue you need to create a landing page and then use its name in global search input field and hit enter then open any other page in new tab. You will see 500 error on every new page that you will open as shown in below screenshot.