|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace IntegerNet\GlobalCustomLayout\Plugin; |
| 5 | + |
| 6 | +use Magento\Catalog\Api\Data\CategoryInterface; |
| 7 | +use Magento\Catalog\Model\Category; |
| 8 | +use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager; |
| 9 | +use Magento\Framework\App\Area; |
| 10 | +use Magento\Framework\DataObject; |
| 11 | +use Magento\Framework\View\Design\Theme\FlyweightFactory; |
| 12 | +use Magento\Framework\View\DesignInterface; |
| 13 | +use Magento\Framework\View\Model\Layout\Merge as LayoutProcessor; |
| 14 | +use Magento\Framework\View\Model\Layout\MergeFactory as LayoutProcessorFactory; |
| 15 | + |
| 16 | +class CategoryLayoutPlugin |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * @var FlyweightFactory |
| 21 | + */ |
| 22 | + private $themeFactory; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var DesignInterface |
| 26 | + */ |
| 27 | + private $design; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var LayoutProcessorFactory |
| 31 | + */ |
| 32 | + private $layoutProcessorFactory; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var LayoutProcessor|null |
| 36 | + */ |
| 37 | + private $layoutProcessor; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param FlyweightFactory $themeFactory |
| 41 | + * @param DesignInterface $design |
| 42 | + * @param LayoutProcessorFactory $layoutProcessorFactory |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + FlyweightFactory $themeFactory, |
| 46 | + DesignInterface $design, |
| 47 | + LayoutProcessorFactory $layoutProcessorFactory |
| 48 | + ) |
| 49 | + { |
| 50 | + $this->themeFactory = $themeFactory; |
| 51 | + $this->design = $design; |
| 52 | + $this->layoutProcessorFactory = $layoutProcessorFactory; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the processor instance. |
| 57 | + * |
| 58 | + * @return LayoutProcessor |
| 59 | + * |
| 60 | + * Unchanged private method copied over from @var LayoutUpdateManager |
| 61 | + */ |
| 62 | + private function getLayoutProcessor(): LayoutProcessor |
| 63 | + { |
| 64 | + if (!$this->layoutProcessor) { |
| 65 | + $this->layoutProcessor = $this->layoutProcessorFactory->create( |
| 66 | + [ |
| 67 | + 'theme' => $this->themeFactory->create( |
| 68 | + $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND) |
| 69 | + ) |
| 70 | + ] |
| 71 | + ); |
| 72 | + $this->themeFactory = null; |
| 73 | + $this->design = null; |
| 74 | + } |
| 75 | + |
| 76 | + return $this->layoutProcessor; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Fetch list of available global files/handles for the category. |
| 81 | + * |
| 82 | + * @param LayoutUpdateManager $subject |
| 83 | + * @param array $result |
| 84 | + * @param CategoryInterface $category |
| 85 | + * @return array |
| 86 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 87 | + */ |
| 88 | + public function afterFetchAvailableFiles( |
| 89 | + LayoutUpdateManager $subject, |
| 90 | + array $result, |
| 91 | + CategoryInterface $category |
| 92 | + ): array |
| 93 | + { |
| 94 | + if (!$category->getId()) { |
| 95 | + return $result; |
| 96 | + } |
| 97 | + |
| 98 | + $handles = $this->getLayoutProcessor()->getAvailableHandles(); |
| 99 | + |
| 100 | + return array_merge($result, array_filter( |
| 101 | + array_map( |
| 102 | + function (string $handle) use ($category) : ?string { |
| 103 | + preg_match( |
| 104 | + '/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i', |
| 105 | + $handle, |
| 106 | + $selectable |
| 107 | + ); |
| 108 | + if (!empty($selectable[1])) { |
| 109 | + return $selectable[1]; |
| 110 | + } |
| 111 | + |
| 112 | + return null; |
| 113 | + }, |
| 114 | + $handles |
| 115 | + ) |
| 116 | + )); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Extract selected global custom layout settings. |
| 121 | + * |
| 122 | + * If no update is selected none will apply. |
| 123 | + * |
| 124 | + * @param LayoutUpdateManager $subject |
| 125 | + * @param $result |
| 126 | + * @param CategoryInterface $category |
| 127 | + * @param DataObject $intoSettings |
| 128 | + * @return void |
| 129 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 130 | + */ |
| 131 | + public function afterExtractCustomSettings( |
| 132 | + LayoutUpdateManager $subject, |
| 133 | + $result, |
| 134 | + CategoryInterface $category, |
| 135 | + DataObject $intoSettings |
| 136 | + ): void |
| 137 | + { |
| 138 | + if ($category->getId() && $value = $this->extractAttributeValue($category)) { |
| 139 | + $handles = $intoSettings->getPageLayoutHandles() ?? []; |
| 140 | + $handles = array_merge_recursive( |
| 141 | + $handles, |
| 142 | + ['selectable_0' => $value] |
| 143 | + ); |
| 144 | + $intoSettings->setPageLayoutHandles($handles); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Extract custom layout attribute value. |
| 150 | + * |
| 151 | + * @param CategoryInterface $category |
| 152 | + * @return mixed |
| 153 | + * |
| 154 | + * Unchanged private method copied over from @var LayoutUpdateManager |
| 155 | + */ |
| 156 | + private function extractAttributeValue(CategoryInterface $category) |
| 157 | + { |
| 158 | + if ($category instanceof Category && !$category->hasData(CategoryInterface::CUSTOM_ATTRIBUTES)) { |
| 159 | + return $category->getData('custom_layout_update_file'); |
| 160 | + } |
| 161 | + if ($attr = $category->getCustomAttribute('custom_layout_update_file')) { |
| 162 | + return $attr->getValue(); |
| 163 | + } |
| 164 | + |
| 165 | + return null; |
| 166 | + } |
| 167 | +} |
0 commit comments