From 8db103f184619cb043344c1142633fcd4484e935 Mon Sep 17 00:00:00 2001 From: Vincent Garcia Date: Thu, 16 Jan 2025 16:39:05 +0100 Subject: [PATCH 1/2] :bug: Do not load recommended modules on edit or detail pages. Due to the Symfony migration on certain pages, we need to ensure that recommended modules (either as buttons or inline) are displayed only on list pages. --- .../UseActionAdminControllerSetMedia.php | 18 ++++++++++++----- src/Traits/Hooks/UseDisplayDashboardTop.php | 20 ++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Traits/Hooks/UseActionAdminControllerSetMedia.php b/src/Traits/Hooks/UseActionAdminControllerSetMedia.php index f6d578283..7e8139358 100644 --- a/src/Traits/Hooks/UseActionAdminControllerSetMedia.php +++ b/src/Traits/Hooks/UseActionAdminControllerSetMedia.php @@ -40,7 +40,7 @@ public function bootUseActionAdminControllerSetMedia(): void /** * Hook actionAdminControllerSetMedia. */ - public function hookActionAdminControllerSetMedia(): void + public function hookActionAdminControllerSetMedia($params): void { if (\Tools::getValue('controller') === 'AdminPsMboModule') { $this->context->controller->addJs( @@ -58,7 +58,7 @@ public function hookActionAdminControllerSetMedia(): void return $a['order'] === $b['order'] ? 0 : $order; }); foreach ($this->adminControllerMediaMethods as $setMediaMethod) { - $this->{$setMediaMethod['method']}(); + $this->{$setMediaMethod['method']}($params); } } @@ -84,9 +84,10 @@ protected function addAdminControllerMedia(string $setMediaMethod, int $order = /** * Add JS and CSS file * + * @param array $hookParams * @return void */ - protected function loadMediaForAdminControllerSetMedia(): void + protected function loadMediaForAdminControllerSetMedia(array $hookParams): void { if (in_array(\Tools::getValue('controller'), self::CONTROLLERS_WITH_CDC_SCRIPT)) { $this->context->controller->addJs('/js/jquery/plugins/growl/jquery.growl.js?v=' . $this->version); @@ -103,10 +104,10 @@ protected function loadMediaForAdminControllerSetMedia(): void // Add it to have all script work on all pages... $this->context->controller->addJs('/admin-dev/themes/default/js/bundle/default.js?v=' . _PS_VERSION_); } - $this->loadCdcMedia(); + $this->loadCdcMedia($hookParams); } - private function loadCdcMedia(): void + private function loadCdcMedia(array $hookParams): void { $controllerName = \Tools::getValue('controller'); if (!is_string($controllerName)) { @@ -118,6 +119,13 @@ private function loadCdcMedia(): void ) { return; } + if( + Tab::mayDisplayRecommendedModules($controllerName) + && $this->isSymfonyContext() && !empty($hookParams['route']) && !str_ends_with($hookParams['route'], '_index') + ) { + return; + } + $this->context->controller->addJs($this->getPathUri() . 'views/js/cdc-error-templating.js'); $this->context->controller->addCss($this->getPathUri() . 'views/css/cdc-error-templating.css'); diff --git a/src/Traits/Hooks/UseDisplayDashboardTop.php b/src/Traits/Hooks/UseDisplayDashboardTop.php index 9a3a09bf2..fa6d61578 100644 --- a/src/Traits/Hooks/UseDisplayDashboardTop.php +++ b/src/Traits/Hooks/UseDisplayDashboardTop.php @@ -85,7 +85,7 @@ public function bootUseDisplayDashboardTop(): void * * @throws \Exception */ - public function hookDisplayDashboardTop(): string + public function hookDisplayDashboardTop($params): string { // Check if this page has already been processed by the hook to avoid duplicate content if ($this->alreadyProcessedPage) { @@ -113,7 +113,7 @@ public function hookDisplayDashboardTop(): string return $shouldDisplayMessageInConfigPage ? $this->displayPushOnConfigurationPage($values['configure']) - : $this->displayRecommendedModules($values['controller'] ?? ''); + : $this->displayRecommendedModules($values['controller'] ?? '', $params); } /** @@ -223,8 +223,12 @@ private function displayFailedApiUser(): string * * @throws \Exception */ - protected function displayRecommendedModules(string $controller): string + protected function displayRecommendedModules(string $controller, array $hookParams): string { + if ($this->isSymfonyContext() && !empty($hookParams['route']) && !str_ends_with($hookParams['route'], '_index')) { + return ''; + } + $recommendedModulesDisplayed = true; // Ask to modules if recommended modules should be displayed in this context @@ -331,15 +335,17 @@ protected function shouldAttachRecommendedModules(string $type): bool * * @see UseActionAdminControllerSetMedia */ - protected function loadMediaForDashboardTop(): void + protected function loadMediaForDashboardTop($hookParams): void { - // has to be loaded in header to prevent flash of content - $this->context->controller->addJs($this->getPathUri() . 'views/js/recommended-modules.js?v=' . $this->version); - + if ($this->isSymfonyContext() && !empty($hookParams['route']) && !str_ends_with($hookParams['route'], '_index')) { + return; + } if ( $this->shouldAttachRecommendedModules(TabInterface::RECOMMENDED_BUTTON_TYPE) || $this->shouldAttachRecommendedModules(TabInterface::RECOMMENDED_AFTER_CONTENT_TYPE) ) { + // has to be loaded in header to prevent flash of content + $this->context->controller->addJs($this->getPathUri() . 'views/js/recommended-modules.js?v=' . $this->version); $this->context->controller->addCSS($this->getPathUri() . 'views/css/recommended-modules.css'); } } From 32832feebbb0d02a23c7b5f3a8a52be6173ef3b6 Mon Sep 17 00:00:00 2001 From: Vincent Garcia Date: Thu, 16 Jan 2025 16:44:13 +0100 Subject: [PATCH 2/2] CS Fixer --- src/Traits/Hooks/UseActionAdminControllerSetMedia.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Traits/Hooks/UseActionAdminControllerSetMedia.php b/src/Traits/Hooks/UseActionAdminControllerSetMedia.php index 7e8139358..05536614e 100644 --- a/src/Traits/Hooks/UseActionAdminControllerSetMedia.php +++ b/src/Traits/Hooks/UseActionAdminControllerSetMedia.php @@ -85,6 +85,7 @@ protected function addAdminControllerMedia(string $setMediaMethod, int $order = * Add JS and CSS file * * @param array $hookParams + * * @return void */ protected function loadMediaForAdminControllerSetMedia(array $hookParams): void @@ -119,14 +120,13 @@ private function loadCdcMedia(array $hookParams): void ) { return; } - if( + if ( Tab::mayDisplayRecommendedModules($controllerName) && $this->isSymfonyContext() && !empty($hookParams['route']) && !str_ends_with($hookParams['route'], '_index') ) { return; } - $this->context->controller->addJs($this->getPathUri() . 'views/js/cdc-error-templating.js'); $this->context->controller->addCss($this->getPathUri() . 'views/css/cdc-error-templating.css');