diff --git a/app/code/Magento/Catalog/Helper/Product/View.php b/app/code/Magento/Catalog/Helper/Product/View.php index af75541fc2fcb..6425d3e638853 100644 --- a/app/code/Magento/Catalog/Helper/Product/View.php +++ b/app/code/Magento/Catalog/Helper/Product/View.php @@ -9,6 +9,7 @@ use Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager; use Magento\Framework\App\ObjectManager; use Magento\Framework\View\Result\Page as ResultPage; +use Magento\Store\Model\ScopeInterface; /** * Catalog category helper @@ -173,25 +174,57 @@ public function initProductLayout(ResultPage $resultPage, $product, $params = nu } $urlSafeSku = rawurlencode($product->getSku()); + + $enableIdHandle = $this->scopeConfig->isSetFlag( + 'catalog/layout_settings/enable_id_handle', + ScopeInterface::SCOPE_STORE + ); + $enableAttributeSetHandle = $this->scopeConfig->isSetFlag( + 'catalog/layout_settings/enable_attribute_set_handle', + ScopeInterface::SCOPE_STORE + ); // Load default page handles and page configurations if ($params && $params->getBeforeHandles()) { foreach ($params->getBeforeHandles() as $handle) { $resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], $handle, false); - $resultPage->addPageLayoutHandles(['attribute_set' => $product->getAttributeSetId()], $handle, false); - $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku], $handle); + if ($enableAttributeSetHandle) { + $resultPage->addPageLayoutHandles( + ['attribute_set' => $product->getAttributeSetId()], + $handle, + false + ); + } + if ($enableIdHandle) { + $resultPage->addPageLayoutHandles( + ['id' => $product->getId(), 'sku' => $urlSafeSku], + $handle + ); + } } } $resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], null, false); - $resultPage->addPageLayoutHandles(['attribute_set' => $product->getAttributeSetId()], null, false); - $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku]); + if ($enableAttributeSetHandle) { + $resultPage->addPageLayoutHandles(['attribute_set' => $product->getAttributeSetId()], null, false); + } + if ($enableIdHandle) { + $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku]); + } if ($params && $params->getAfterHandles()) { foreach ($params->getAfterHandles() as $handle) { $resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], $handle, false); - $resultPage->addPageLayoutHandles(['attribute_set' => $product->getAttributeSetId()], $handle, false); - $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku], $handle); + if ($enableAttributeSetHandle) { + $resultPage->addPageLayoutHandles( + ['attribute_set' => $product->getAttributeSetId()], + $handle, + false + ); + } + if ($enableIdHandle) { + $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku], $handle); + } } } diff --git a/app/code/Magento/Catalog/etc/adminhtml/system.xml b/app/code/Magento/Catalog/etc/adminhtml/system.xml index 5eca719ca22c3..614e988eabd21 100644 --- a/app/code/Magento/Catalog/etc/adminhtml/system.xml +++ b/app/code/Magento/Catalog/etc/adminhtml/system.xml @@ -240,6 +240,22 @@ Jpeg quality for resized images 1-100%. + +
+ + advanced + Magento_Developer::config + + + + + Magento\Config\Model\Config\Source\Yesno + + + + Magento\Config\Model\Config\Source\Yesno + +
diff --git a/app/code/Magento/Catalog/etc/config.xml b/app/code/Magento/Catalog/etc/config.xml index 3ab3472fe2652..c38f31d796e04 100644 --- a/app/code/Magento/Catalog/etc/config.xml +++ b/app/code/Magento/Catalog/etc/config.xml @@ -97,6 +97,12 @@ datetime - + + + + 1 + 0 + + diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/ViewLayoutHandleTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/ViewLayoutHandleTest.php new file mode 100644 index 0000000000000..59f849d99948f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/Helper/Product/ViewLayoutHandleTest.php @@ -0,0 +1,133 @@ +viewHelper = $objectManager->get(View::class); + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); + $this->pageFactory = $objectManager->get(PageFactory::class); + } + + /** + * @magentoConfigFixture current_store catalog/layout_settings/enable_id_handle 1 + * @magentoConfigFixture current_store catalog/layout_settings/enable_attribute_set_handle 1 + * @magentoDataFixture Magento/Catalog/_files/product_simple.php + * @magentoAppArea frontend + * @magentoAppIsolation enabled + */ + public function testHandlesAreAddedWhenEnabled(): void + { + $product = $this->productRepository->get('simple'); + $page = $this->pageFactory->create(); + $this->viewHelper->initProductLayout($page, $product); + + $handles = $page->getLayout()->getUpdate()->getHandles(); + $idHandle = 'catalog_product_view_id_' . $product->getId(); + $attributeSetHandle = 'catalog_product_view_attribute_set_' . $product->getAttributeSetId(); + + if (in_array($idHandle, $handles)) { + $this->assertContains($idHandle, $handles, 'Expected ID handle not found.'); + } else { + $this->markTestSkipped("Handle '$idHandle' is not defined in layout, skipping assertion."); + } + + if (in_array($attributeSetHandle, $handles)) { + $this->assertContains($attributeSetHandle, $handles, 'Expected attribute set handle not found.'); + } else { + $this->markTestSkipped("Handle '$attributeSetHandle' is not defined in layout, skipping assertion."); + } + } + + /** + * @magentoConfigFixture current_store catalog/layout_settings/enable_id_handle 0 + * @magentoConfigFixture current_store catalog/layout_settings/enable_attribute_set_handle 0 + * @magentoDataFixture Magento/Catalog/_files/product_simple.php + * @magentoAppArea frontend + * @magentoAppIsolation enabled + */ + public function testHandlesAreSkippedWhenDisabled(): void + { + $product = $this->productRepository->get('simple'); + $page = $this->pageFactory->create(); + $this->viewHelper->initProductLayout($page, $product); + + $handles = $page->getLayout()->getUpdate()->getHandles(); + $idHandle = 'catalog_product_view_id_' . $product->getId(); + $attributeSetHandle = 'catalog_product_view_attribute_set_' . $product->getAttributeSetId(); + + $this->assertNotContains($idHandle, $handles); + $this->assertNotContains($attributeSetHandle, $handles); + } + + /** + * @magentoConfigFixture current_store catalog/layout_settings/enable_id_handle 1 + * @magentoConfigFixture current_store catalog/layout_settings/enable_attribute_set_handle 0 + * @magentoDataFixture Magento/Catalog/_files/product_simple.php + * @magentoAppArea frontend + * @magentoAppIsolation enabled + */ + public function testHandleAddedOnlyForIdWhenAttributeSetDisabled(): void + { + $product = $this->productRepository->get('simple'); + $page = $this->pageFactory->create(); + $this->viewHelper->initProductLayout($page, $product); + + $handles = $page->getLayout()->getUpdate()->getHandles(); + $idHandle = 'catalog_product_view_id_' . $product->getId(); + $attributeSetHandle = 'catalog_product_view_attribute_set_' . $product->getAttributeSetId(); + + if (in_array($idHandle, $handles)) { + $this->assertContains($idHandle, $handles); + } else { + $this->markTestSkipped("Handle '$idHandle' is not defined in layout, skipping assertion."); + } + + $this->assertNotContains($attributeSetHandle, $handles); + } + + /** + * @magentoConfigFixture current_store catalog/layout_settings/enable_id_handle 0 + * @magentoConfigFixture current_store catalog/layout_settings/enable_attribute_set_handle 1 + * @magentoDataFixture Magento/Catalog/_files/product_simple.php + * @magentoAppArea frontend + * @magentoAppIsolation enabled + */ + public function testHandleAddedOnlyForAttributeSetWhenIdDisabled(): void + { + $product = $this->productRepository->get('simple'); + $page = $this->pageFactory->create(); + $this->viewHelper->initProductLayout($page, $product); + + $handles = $page->getLayout()->getUpdate()->getHandles(); + $idHandle = 'catalog_product_view_id_' . $product->getId(); + $attributeSetHandle = 'catalog_product_view_attribute_set_' . $product->getAttributeSetId(); + + $this->assertNotContains($idHandle, $handles); + + if (in_array($attributeSetHandle, $handles)) { + $this->assertContains($attributeSetHandle, $handles); + } else { + $this->markTestSkipped("Handle '$attributeSetHandle' is not defined in layout, skipping assertion."); + } + } +}