0

I want separate layout handles defined for products dependent on their layout set via the layout options for product within admin (1 column, 2 column left, 2 column right).

I need to set additional instructions to move and remove blocks based on the layout set. For example set a product to 1 column and update the catalog_product_view_layout_1column.xml file to remove blocks.

I have tried with both an observer and plugin to try and set this additional handle however am having difficulty. Below is how I tried to achieve with plugin method:

<type name="Magento\Catalog\Helper\Product\View">
    <plugin name="Vendor_Module_LayoutMods" type="Vendor\Module\Plugin\LayoutMod" />
</type>  

di.xml

<?php

namespace Vendor\Module\Plugin;

class LayoutMod
{
    public function aroundinitProductLayout(
        \Magento\Catalog\Helper\Product\View $subject,
        callable $proceed,
        \Magento\Framework\View\Result\Page $resultPage,
        $product,
        $params = null
    ) {
        $result = $proceed($resultPage, $product, $params);
        $resultPage->addPageLayoutHandles(['layout' => $product->getData('page_layout')], 'catalog_product_view');
        return $result;
    }
}

plugin/LayoutMod.php

This seems to add the handle in that $resultPage->getLayout()->getUpdate()->getHandles() shows my new custom handle catalog_product_view_layout_1column catalog_product_view_layout_1column.xml does not seem to be processed. catalog_product_view_id_123.xml does work however fine and blocks are removed. My xml is currently pretty simple as I test:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <attribute name="class" value="my-css-class"/>
        <referenceBlock name="product.info.main" remove="true"/>
    </body>
</page>

Magento_Catalog/layout/catalog_product_view_layout_1column.xml

I'm wondering can help explain why my new handle is not working?

0

The below seemed to work for me, the key was modifying what was returned:

   class LayoutMod
   {
      public function beforeInitProductLayout(
          \Magento\Catalog\Helper\Product\View $subject,
          \Magento\Framework\View\Result\Page $resultPage, 
          \Magento\Catalog\Model\Product $product, 
          $params = null
      ) {
          $resultPage->addPageLayoutHandles(['layout' => $product->getData('page_layout')], 'catalog_product_view');
          return [$resultPage, $product, $params];
      }
   }
0

I know you solved your issue, but for others coming across this, I'd recommend this article for a full-writeup of how to do this https://www.atwix.com/magento-2/add-custom-layout-handle-to-product-page-magento-2/

Copying relevant content here for posterity's sake:

file: [Vendor][Namespace]\Plugin\Catalog\Product\AddCustomLayoutHandleToProductPlugin.php:

<?php
/**
 * @author    Atwix Team
 * @copyright Copyright (c) 2018 Atwix (https://www.atwix.com/)
 */
namespace [Vendor]\[Namespace]\Plugin\Catalog\Product;
use Magento\Catalog\Helper\Product\View as ProductViewHelper;
use Magento\Catalog\Model\Product;
use Magento\Framework\DataObject;
use Magento\Framework\View\Result\Page as ResultPage;
use Magento\Framework\View\Result\Page;
/**
 *  AddCustomLayoutHandleToProductPlugin
 */
class AddCustomLayoutHandleToProductPlugin
{
    const PRODUCT_LAYOUT_HANDLE = 'product_page_with_custom_handle';
    /**
     * @param  ProductViewHelper $subject
     * @param  Page            $resultPage
     * @param  Product         $product
     * @param  null|DataObject $params
     * @return array
     */
    public function beforeInitProductLayout(
        ProductViewHelper $subject,
        $resultPage,
        $product,
        $params
    ) {
        if ($resultPage instanceof ResultPage && /* your additional checks here */) {
            $resultPage->addHandle([static::PRODUCT_LAYOUT_HANDLE]);
        }
        return [
            $resultPage,
            $product,
            $params
        ];
    }
}

file: [Vendor]/[Namespace]/etc/frontend/di.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!--
/**
 * @author Atwix Team
 * @copyright Copyright (c) 2018 Atwix (https://www.atwix.com/)
 */-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Helper\Product\View">
        <plugin name="add_custom_layout_handle_to_product_page"
                type="[Vendor]\[Namespace]\Plugin\Catalog\Product\AddCustomLayoutHandleToProductPlugin"/>
    </type>
</config>

Based on the product_page_with_custom_handle handle name in the class above, you will now be able to create a product_page_with_custom_handle.xml file in a theme or module and it should get loaded when the your additional checks here logic is satisfied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.