|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Backend\Block\Widget\Form\Element; |
| 9 | + |
| 10 | +use Magento\Eav\Model\Entity\Attribute; |
| 11 | +use Magento\Framework\Data\Form\Element\AbstractElement; |
| 12 | +use Magento\Framework\Data\Form\Element\Fieldset; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class ElementCreator |
| 16 | + * |
| 17 | + * @deprecated 100.3.0 in favour of UI component implementation |
| 18 | + * @package Magento\Backend\Block\Widget\Form\Element |
| 19 | + */ |
| 20 | +class ElementCreator |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + private $modifiers; |
| 26 | + |
| 27 | + /** |
| 28 | + * ElementCreator constructor. |
| 29 | + * |
| 30 | + * @param array $modifiers |
| 31 | + */ |
| 32 | + public function __construct(array $modifiers = []) |
| 33 | + { |
| 34 | + $this->modifiers = $modifiers; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates element |
| 39 | + * |
| 40 | + * @param Fieldset $fieldset |
| 41 | + * @param Attribute $attribute |
| 42 | + * |
| 43 | + * @return AbstractElement |
| 44 | + */ |
| 45 | + public function create(Fieldset $fieldset, Attribute $attribute): AbstractElement |
| 46 | + { |
| 47 | + $config = $this->getElementConfig($attribute); |
| 48 | + |
| 49 | + if (!empty($config['rendererClass'])) { |
| 50 | + $fieldType = $config['inputType'] . '_' . $attribute->getAttributeCode(); |
| 51 | + $fieldset->addType($fieldType, $config['rendererClass']); |
| 52 | + } |
| 53 | + |
| 54 | + return $fieldset |
| 55 | + ->addField($config['attribute_code'], $config['inputType'], $config) |
| 56 | + ->setEntityAttribute($attribute); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns element config |
| 61 | + * |
| 62 | + * @param Attribute $attribute |
| 63 | + * @return array |
| 64 | + */ |
| 65 | + private function getElementConfig(Attribute $attribute): array |
| 66 | + { |
| 67 | + $defaultConfig = $this->createDefaultConfig($attribute); |
| 68 | + $config = $this->modifyConfig($defaultConfig); |
| 69 | + |
| 70 | + $config['label'] = __($config['label']); |
| 71 | + |
| 72 | + return $config; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns default config |
| 77 | + * |
| 78 | + * @param Attribute $attribute |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + private function createDefaultConfig(Attribute $attribute): array |
| 82 | + { |
| 83 | + return [ |
| 84 | + 'inputType' => $attribute->getFrontend()->getInputType(), |
| 85 | + 'rendererClass' => $attribute->getFrontend()->getInputRendererClass(), |
| 86 | + 'attribute_code' => $attribute->getAttributeCode(), |
| 87 | + 'name' => $attribute->getAttributeCode(), |
| 88 | + 'label' => $attribute->getFrontend()->getLabel(), |
| 89 | + 'class' => $attribute->getFrontend()->getClass(), |
| 90 | + 'required' => $attribute->getIsRequired(), |
| 91 | + 'note' => $attribute->getNote(), |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Modify config |
| 97 | + * |
| 98 | + * @param array $config |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + private function modifyConfig(array $config): array |
| 102 | + { |
| 103 | + if ($this->isModified($config['attribute_code'])) { |
| 104 | + return $this->applyModifier($config); |
| 105 | + } |
| 106 | + return $config; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Returns bool if attribute need to modify |
| 111 | + * |
| 112 | + * @param string $attribute_code |
| 113 | + * @return bool |
| 114 | + */ |
| 115 | + private function isModified($attribute_code): bool |
| 116 | + { |
| 117 | + return isset($this->modifiers[$attribute_code]); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Apply modifier to config |
| 122 | + * |
| 123 | + * @param array $config |
| 124 | + * @return array |
| 125 | + */ |
| 126 | + private function applyModifier(array $config): array |
| 127 | + { |
| 128 | + $modifiedConfig = $this->modifiers[$config['attribute_code']]; |
| 129 | + foreach (array_keys($config) as $key) { |
| 130 | + if (isset($modifiedConfig[$key])) { |
| 131 | + $config[$key] = $modifiedConfig[$key]; |
| 132 | + } |
| 133 | + } |
| 134 | + return $config; |
| 135 | + } |
| 136 | +} |
0 commit comments