Snippets

CreativeMinds Article / M2 Layout update incompatibility

Created by Piotr Pierzak last modified
<?php

namespace Namespace\To\Our\Plugin;

use Closure;
use Vendor\Extension\Model\Config as ModuleConfig;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ProductMetadata;
use Magento\Framework\Filesystem;
use Magento\Framework\Registry as CoreRegistry;
use Magento\Framework\View\Design\ThemeInterface;
use Magento\Framework\View\Layout\File\Collector\Aggregated;

class GetKey
{
    /**
     * Core registry object.
     *
     * @var CoreRegistry
     */
    protected $coreRegistry;

    /**
     * @var Filesystem
     */
    protected $filesystem;

    /**
     * Product meta data object.
     *
     * @var ProductMetadata
     */
    protected $productMetadata;

    /**
     * Object constructor.
     *
     * @param CoreRegistry    $coreRegistry    Core registry object.
     * @param Filesystem      $filesystem      File system object.
     * @param ProductMetadata $productMetadata Product meta data object.
     */
    public function __construct(
        CoreRegistry $coreRegistry,
        Filesystem $filesystem,
        ProductMetadata $productMetadata
    ) {
        $this->coreRegistry = $coreRegistry;
        $this->filesystem = $filesystem;
        $this->productMetadata = $productMetadata;
    }

    /**
     * Plugin for getFiles method. Get order archive layout update file index
     * and save it in registry to be able to recognize it in further processing.
     * It will be applied for magento version lower than 2.1.
     *
     * @param Aggregated     $subject  Subject object.
     * @param Closure        $proceed  Closure object.
     * @param ThemeInterface $theme    Theme object.
     * @param string         $filePath File path.
     *
     * @return array
     */
    public function aroundGetFiles(
        Aggregated $subject,
        Closure $proceed,
        ThemeInterface $theme,
        $filePath
    ) {
        $files = $proceed($theme, $filePath);
        if ($filePath !== 'sales_order_grid.xml') {
            return $files;
        }

        $version = $this->productMetadata->getVersion();
        if (version_compare($version, '2.1') >= 0) {
            return $files;
        }

        $fileReader = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);

        foreach ($files as $index => $file) {
            if ($file->getModule() !== 'Vendor_Extension') {
                continue;
            }

            $filePath = $fileReader->getRelativePath($file->getFilename());
            $fileKey = sprintf('%x', crc32($filePath));

            $this->coreRegistry->register(
                ModuleConfig::REGISTRY_SALES_ORDER_GRID_FILE_INDEX,
                $fileKey
            );
        }

        return $files;
    }
}
<?php

namespace Namespace\To\Our\Plugin;

use Magento\Framework\Filesystem;
use Magento\Framework\Registry as CoreRegistry;
use Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollector;
use Vendor\Extension\Model\Config as ModuleConfig;

class Replace
{
    /**
     * Core registry object.
     *
     * @var CoreRegistry
     */
    protected $coreRegistry;

    /**
     * Object constructor.
     *
     * @param CoreRegistry $coreRegistry Core registry object.
     */
    public function __construct(
        CoreRegistry $coreRegistry
    ) {
        $this->coreRegistry = $coreRegistry;
    }

    /**
     * Plugin for collectFiles method. For magento version lower than 2.1
     * replace listingToolbar tag to container in sales_order_grid layout update.
     *
     * @param AggregatedFileCollector $subject Subject object.
     * @param array                   $result  Result array.
     *
     * @return array
     */
    public function afterCollectFiles(
        AggregatedFileCollector $subject,
        array $result
    ) {
        $fileKey = $this->coreRegistry
            ->registry(ModuleConfig::REGISTRY_SALES_ORDER_GRID_FILE_INDEX);

        if ($fileKey === null) {
            return $result;
        }
        if (empty($result[$fileKey])) {
            return $result;
        }

        $this->coreRegistry
            ->unregister(ModuleConfig::REGISTRY_SALES_ORDER_GRID_FILE_INDEX);

        $result[$fileKey] = str_replace(
            'listingToolbar',
            'container',
            $result[$fileKey]
        );

        return $result;
    }
}
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Layout\File\Collector\Aggregated">
        <plugin name="samplePluginNameGetKey"
                type="\Namespace\To\Our\Plugin\GetKey"
                sortOrder="10"/>
    </type>
    <type name="Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollector">
        <plugin name="samplePluginNameReplace"
                type="\Namespace\To\Our\Plugin\Replace"
                sortOrder="10"/>
    </type>
</config>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.