Field.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component\Form;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Ui\Component\AbstractComponent;
  9. use Magento\Framework\View\Element\UiComponentFactory;
  10. use Magento\Framework\View\Element\UiComponentInterface;
  11. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  12. /**
  13. * Class Field
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Field extends AbstractComponent
  18. {
  19. const NAME = 'field';
  20. /**
  21. * Wrapped component
  22. *
  23. * @var UiComponentInterface
  24. */
  25. protected $wrappedComponent;
  26. /**
  27. * UI component factory
  28. *
  29. * @var UiComponentFactory
  30. */
  31. protected $uiComponentFactory;
  32. /**
  33. * Constructor
  34. *
  35. * @param ContextInterface $context
  36. * @param UiComponentFactory $uiComponentFactory
  37. * @param UiComponentInterface[] $components
  38. * @param array $data
  39. */
  40. public function __construct(
  41. ContextInterface $context,
  42. UiComponentFactory $uiComponentFactory,
  43. array $components = [],
  44. array $data = []
  45. ) {
  46. $this->uiComponentFactory = $uiComponentFactory;
  47. parent::__construct($context, $components, $data);
  48. }
  49. /**
  50. * Get component name
  51. *
  52. * @return string
  53. */
  54. public function getComponentName()
  55. {
  56. return 'form.' . $this->wrappedComponent->getComponentName();
  57. }
  58. /**
  59. * Prepare component configuration
  60. *
  61. * @return void
  62. * @throws \Magento\Framework\Exception\LocalizedException
  63. */
  64. public function prepare()
  65. {
  66. $formElement = $this->getData('config/formElement');
  67. if (null === $formElement) {
  68. throw new LocalizedException(__(
  69. 'The "formElement" configuration parameter is required for the "%1" field.',
  70. $this->getName()
  71. ));
  72. }
  73. // Create of wrapped component
  74. $this->wrappedComponent = $this->uiComponentFactory->create(
  75. $this->getName(),
  76. $formElement,
  77. array_merge(['context' => $this->getContext()], (array)$this->getData())
  78. );
  79. $this->wrappedComponent->setData(
  80. 'config',
  81. array_replace_recursive(
  82. ['dataScope' => $this->getName()],
  83. (array) $this->wrappedComponent->getData('config'),
  84. (array) $this->getData('config')
  85. )
  86. );
  87. foreach ($this->components as $nameComponent => $component) {
  88. $this->wrappedComponent->addComponent($nameComponent, $component);
  89. }
  90. $this->prepareChildComponent($this->wrappedComponent);
  91. $this->components = $this->wrappedComponent->getChildComponents();
  92. // Merge JS configuration with wrapped component configuration
  93. $wrappedComponentConfig = $this->getJsConfig($this->wrappedComponent);
  94. $jsConfig = array_replace_recursive($wrappedComponentConfig, $this->getJsConfig($this));
  95. $jsConfig['extends'] = $this->wrappedComponent->getComponentName();
  96. $this->setData('js_config', $jsConfig);
  97. $this->setData('config', $this->wrappedComponent->getData('config'));
  98. parent::prepare();
  99. }
  100. }