Multiline.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Component\Form\Element;
  7. use Magento\Ui\Component\Form\Field;
  8. use Magento\Framework\View\Element\UiComponentFactory;
  9. use Magento\Framework\View\Element\UiComponentInterface;
  10. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Multiline extends AbstractElement
  16. {
  17. const NAME = 'multiline';
  18. const FORM_ELEMENT = 'input';
  19. const DATA_TYPE = 'text';
  20. /**
  21. * UI component factory
  22. *
  23. * @var UiComponentFactory
  24. */
  25. protected $uiComponentFactory;
  26. /**
  27. * Constructor
  28. *
  29. * @param ContextInterface $context
  30. * @param UiComponentFactory $uiComponentFactory
  31. * @param UiComponentInterface[] $components
  32. * @param array $data
  33. */
  34. public function __construct(
  35. ContextInterface $context,
  36. UiComponentFactory $uiComponentFactory,
  37. array $components = [],
  38. array $data = []
  39. ) {
  40. $this->uiComponentFactory = $uiComponentFactory;
  41. parent::__construct($context, $components, $data);
  42. }
  43. /**
  44. * Get component name
  45. *
  46. * @return string
  47. */
  48. public function getComponentName()
  49. {
  50. return static::NAME;
  51. }
  52. /**
  53. * Prepare component configuration
  54. *
  55. * @return void
  56. */
  57. public function prepare()
  58. {
  59. $size = abs((int) $this->getData('config/size'));
  60. $validation = [$this->getData('config/validation')];
  61. while ($size--) {
  62. $identifier = $this->getName() . '_' . $size;
  63. $arguments = [
  64. 'data' => [
  65. 'name' => $identifier,
  66. 'config' => [
  67. 'dataScope' => $size,
  68. 'dataType' => static::DATA_TYPE,
  69. 'formElement' => static::FORM_ELEMENT,
  70. 'sortOrder' => $size,
  71. ]
  72. ]
  73. ];
  74. if (!empty($validation[$size])) {
  75. $arguments['data']['config']['validation'] = $validation[$size];
  76. }
  77. $component = $this->uiComponentFactory->create($identifier, Field::NAME, $arguments);
  78. $component->prepare();
  79. $this->components[$identifier] = $component;
  80. }
  81. parent::prepare();
  82. }
  83. }