Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Field.php
Go to the documentation of this file.
1 <?php
8 
14 
20 class Field extends AbstractComponent
21 {
22  const NAME = 'field';
23 
29  protected $wrappedComponent;
30 
37 
46  public function __construct(
49  array $components = [],
50  array $data = []
51  ) {
52  $this->uiComponentFactory = $uiComponentFactory;
53  parent::__construct($context, $components, $data);
54  }
55 
61  public function getComponentName()
62  {
63  return 'form.' . $this->wrappedComponent->getComponentName();
64  }
65 
72  public function prepare()
73  {
74  $formElement = $this->getData('config/formElement');
75  if (null === $formElement) {
76  throw new LocalizedException(__(
77  'The "formElement" configuration parameter is required for the "%1" field.',
78  $this->getName()
79  ));
80  }
81  // Create of wrapped component
82  $this->wrappedComponent = $this->uiComponentFactory->create(
83  $this->getName(),
84  $formElement,
85  array_merge(['context' => $this->getContext()], (array)$this->getData())
86  );
87  $this->wrappedComponent->setData(
88  'config',
89  array_replace_recursive(
90  ['dataScope' => $this->getName()],
91  (array) $this->wrappedComponent->getData('config'),
92  (array) $this->getData('config')
93  )
94  );
95 
96  foreach ($this->components as $nameComponent => $component) {
97  $this->wrappedComponent->addComponent($nameComponent, $component);
98  }
99  $this->prepareChildComponent($this->wrappedComponent);
100 
101  $this->components = $this->wrappedComponent->getChildComponents();
102  // Merge JS configuration with wrapped component configuration
103  $wrappedComponentConfig = $this->getJsConfig($this->wrappedComponent);
104 
105  $jsConfig = array_replace_recursive($wrappedComponentConfig, $this->getJsConfig($this));
106  $jsConfig['extends'] = $this->wrappedComponent->getComponentName();
107  $this->setData('js_config', $jsConfig);
108 
109  $this->setData('config', $this->wrappedComponent->getData('config'));
110 
111  parent::prepare();
112  }
113 }
getJsConfig(UiComponentInterface $component)
prepareChildComponent(UiComponentInterface $component)
__()
Definition: __.php:13
__construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, array $components=[], array $data=[])
Definition: Field.php:46