Wysiwyg.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Framework\Data\Form\Element\Editor;
  8. use Magento\Framework\Data\Form;
  9. use Magento\Framework\Data\FormFactory;
  10. use Magento\Framework\DataObject;
  11. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  12. use Magento\Ui\Component\Wysiwyg\ConfigInterface;
  13. /**
  14. * WYSIWYG form element
  15. *
  16. * @api
  17. * @since 100.1.0
  18. */
  19. class Wysiwyg extends AbstractElement
  20. {
  21. const NAME = 'wysiwyg';
  22. /**
  23. * @var Form
  24. * @since 100.1.0
  25. */
  26. protected $form;
  27. /**
  28. * @var Editor
  29. * @since 100.1.0
  30. */
  31. protected $editor;
  32. /**
  33. * @param ContextInterface $context
  34. * @param FormFactory $formFactory
  35. * @param ConfigInterface $wysiwygConfig
  36. * @param array $components
  37. * @param array $data
  38. * @param array $config
  39. */
  40. public function __construct(
  41. ContextInterface $context,
  42. FormFactory $formFactory,
  43. ConfigInterface $wysiwygConfig,
  44. array $components = [],
  45. array $data = [],
  46. array $config = []
  47. ) {
  48. $wysiwygConfigData = isset($config['wysiwygConfigData']) ? $config['wysiwygConfigData'] : [];
  49. $this->form = $formFactory->create();
  50. $wysiwygId = $context->getNamespace() . '_' . $data['name'];
  51. $this->editor = $this->form->addField(
  52. $wysiwygId,
  53. \Magento\Framework\Data\Form\Element\Editor::class,
  54. [
  55. 'force_load' => true,
  56. 'rows' => isset($config['rows']) ? $config['rows'] : 20,
  57. 'name' => $data['name'],
  58. 'config' => $wysiwygConfig->getConfig($wysiwygConfigData),
  59. 'wysiwyg' => isset($config['wysiwyg']) ? $config['wysiwyg'] : null,
  60. ]
  61. );
  62. $data['config']['content'] = $this->editor->getElementHtml();
  63. $data['config']['wysiwygId'] = $wysiwygId;
  64. parent::__construct($context, $components, $data);
  65. }
  66. /**
  67. * Get component name
  68. *
  69. * @return string
  70. * @since 100.1.0
  71. */
  72. public function getComponentName()
  73. {
  74. return static::NAME;
  75. }
  76. }