Skip to content

Commit 3e775db

Browse files
authored
fix(plugin): prevent undefined method error in MetaRobotsPlugin by adding instance check
fix(plugin): prevent undefined method error in MetaRobotsPlugin by adding instance check **Description:** - Added a check in `hasAppliedFilters()` to ensure `$resultPage` is an instance of `Magento\Framework\View\Result\Page` before accessing layout methods. This resolves the critical error where `getLayout()` was called on incompatible result types like `Forward`. - Updated PHPDoc comments and code structure for better clarity and maintainability. - Ensured compatibility with Magento 2.4.8 and PHP 8.3 standards.
2 parents 9c0b075 + ccc5b02 commit 3e775db

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

Plugin/MetaRobotsPlugin.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,39 @@
99
*
1010
* @category Amadeco
1111
* @package Amadeco_SmileCustomEntityLayeredNavigation
12-
* @copyright Copyright (c) Amadeco (https://www.amadeco.fr) - Ilan Parmentier
12+
* @copyright Copyright (c) Amadeco[](https://www.amadeco.fr) - Ilan Parmentier
1313
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
1414
*/
1515
declare(strict_types=1);
1616

1717
namespace Amadeco\SmileCustomEntityLayeredNavigation\Plugin;
1818

19-
use Smile\CustomEntity\Controller\Set\View;
2019
use Magento\Framework\App\RequestInterface;
20+
use Magento\Framework\Controller\ResultInterface;
2121
use Magento\Framework\View\Page\Config as PageConfig;
2222
use Magento\Framework\View\Result\Page;
23+
use Smile\CustomEntity\Controller\Set\View;
2324

2425
/**
25-
* Plugin to set NOINDEX,FOLLOW meta robots on filtered pages
26+
* Plugin to set NOINDEX,FOLLOW meta robots on filtered custom entity pages.
2627
*/
2728
class MetaRobotsPlugin
2829
{
2930
/**
3031
* @var RequestInterface
3132
*/
32-
private RequestInterface $request;
33+
private readonly RequestInterface $request;
3334

3435
/**
3536
* @var PageConfig
3637
*/
37-
private PageConfig $pageConfig;
38+
private readonly PageConfig $pageConfig;
3839

3940
/**
40-
* @param RequestInterface $request
41-
* @param PageConfig $pageConfig
41+
* Constructor.
42+
*
43+
* @param RequestInterface $request The HTTP request object.
44+
* @param PageConfig $pageConfig The page configuration object.
4245
*/
4346
public function __construct(
4447
RequestInterface $request,
@@ -49,14 +52,14 @@ public function __construct(
4952
}
5053

5154
/**
52-
* Execute
55+
* Sets meta robots to NOINDEX,FOLLOW if filters are applied on custom entity pages.
5356
*
54-
* @param View $subject
55-
* @param Page $page
57+
* @param View $subject The original controller action.
58+
* @param ResultInterface $resultPage The result from the controller execution.
5659
*
57-
* @return mixed
60+
* @return ResultInterface The modified or original result page.
5861
*/
59-
public function afterExecute(View $subject, $resultPage)
62+
public function afterExecute(View $subject, ResultInterface $resultPage): ResultInterface
6063
{
6164
if (!$this->isCustomEntityPage()) {
6265
return $resultPage;
@@ -70,33 +73,41 @@ public function afterExecute(View $subject, $resultPage)
7073
}
7174

7275
/**
73-
* Check if current page is a custom entity page
76+
* Checks if the current page is a custom entity page.
7477
*
75-
* @return bool
78+
* @return bool True if it's a custom entity page, false otherwise.
7679
*/
7780
private function isCustomEntityPage(): bool
7881
{
7982
$moduleName = $this->request->getModuleName();
8083
$controllerName = $this->request->getControllerName();
84+
8185
return $moduleName === 'custom_entity' && $controllerName === 'set';
8286
}
83-
87+
8488
/**
85-
* Check if filters are applied to the current page
89+
* Checks if filters are applied to the current page.
90+
*
91+
* @param ResultInterface $resultPage The controller result object.
8692
*
87-
* @param ResultInterface $resultPage
88-
* @return bool
93+
* @return bool True if filters are applied, false otherwise.
8994
*/
90-
private function hasAppliedFilters($resultPage): bool
95+
private function hasAppliedFilters(ResultInterface $resultPage): bool
9196
{
97+
if (!$resultPage instanceof Page) {
98+
return false;
99+
}
100+
92101
$layout = $resultPage->getLayout();
93102
if (!$layout) {
94103
return false;
95104
}
105+
96106
$state = $layout->getBlock('set.layer.state');
97107
if ($state && $state->getActiveFilters()) {
98108
return true;
99109
}
110+
100111
return false;
101112
}
102113
}

0 commit comments

Comments
 (0)