Form.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * WYSIWYG widget plugin form
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Widget\Block\Adminhtml\Widget;
  12. class Form extends \Magento\Backend\Block\Widget\Form\Generic
  13. {
  14. /**
  15. * @var \Magento\Widget\Model\WidgetFactory
  16. */
  17. protected $_widgetFactory;
  18. /**
  19. * @param \Magento\Backend\Block\Template\Context $context
  20. * @param \Magento\Framework\Registry $registry
  21. * @param \Magento\Framework\Data\FormFactory $formFactory
  22. * @param \Magento\Widget\Model\WidgetFactory $widgetFactory
  23. * @param array $data
  24. */
  25. public function __construct(
  26. \Magento\Backend\Block\Template\Context $context,
  27. \Magento\Framework\Registry $registry,
  28. \Magento\Framework\Data\FormFactory $formFactory,
  29. \Magento\Widget\Model\WidgetFactory $widgetFactory,
  30. array $data = []
  31. ) {
  32. $this->_widgetFactory = $widgetFactory;
  33. parent::__construct($context, $registry, $formFactory, $data);
  34. }
  35. /**
  36. * Form with widget to select
  37. *
  38. * @return void
  39. */
  40. protected function _prepareForm()
  41. {
  42. /** @var \Magento\Framework\Data\Form $form */
  43. $form = $this->_formFactory->create();
  44. // Add messages container to fieldset
  45. $fieldset = $form->addFieldset('base_fieldset', ['legend' => '<div data-role="messages"></div>',
  46. 'comment' => __('Inserting a widget does not create a widget instance.')]);
  47. $fieldset->addField(
  48. 'select_widget_type',
  49. 'select',
  50. [
  51. 'label' => __('Widget Type'),
  52. 'title' => __('Widget Type'),
  53. 'name' => 'widget_type',
  54. 'required' => true,
  55. 'onchange' => "wWidget.validateField()",
  56. 'options' => $this->_getWidgetSelectOptions(),
  57. 'after_element_html' => $this->_getWidgetSelectAfterHtml()
  58. ]
  59. );
  60. $form->setUseContainer(true);
  61. $form->setId('widget_options_form');
  62. $form->setMethod('post');
  63. $form->setAction($this->getUrl('adminhtml/*/buildWidget'));
  64. $this->setForm($form);
  65. }
  66. /**
  67. * Prepare options for widgets HTML select
  68. *
  69. * @return array
  70. */
  71. protected function _getWidgetSelectOptions()
  72. {
  73. foreach ($this->_getAvailableWidgets(true) as $data) {
  74. $options[$data['type']] = $data['name'];
  75. }
  76. return $options;
  77. }
  78. /**
  79. * Prepare widgets select after element HTML
  80. *
  81. * @return string
  82. */
  83. protected function _getWidgetSelectAfterHtml()
  84. {
  85. $html = '<p class="nm"><small></small></p>';
  86. $i = 0;
  87. foreach ($this->_getAvailableWidgets(true) as $data) {
  88. $html .= sprintf('<div id="widget-description-%s" class="no-display">%s</div>', $i, $data['description']);
  89. $i++;
  90. }
  91. return $html;
  92. }
  93. /**
  94. * Return array of available widgets based on configuration
  95. *
  96. * @param bool $withEmptyElement
  97. * @return array
  98. */
  99. protected function _getAvailableWidgets($withEmptyElement = false)
  100. {
  101. if (!$this->hasData('available_widgets')) {
  102. $result = [];
  103. $allWidgets = $this->_widgetFactory->create()->getWidgetsArray();
  104. $skipped = $this->_getSkippedWidgets();
  105. foreach ($allWidgets as $widget) {
  106. if (is_array($skipped) && in_array($widget['type'], $skipped)) {
  107. continue;
  108. }
  109. $result[] = $widget;
  110. }
  111. if ($withEmptyElement) {
  112. array_unshift($result, ['type' => '', 'name' => __('-- Please Select --'), 'description' => '']);
  113. }
  114. $this->setData('available_widgets', $result);
  115. }
  116. return $this->_getData('available_widgets');
  117. }
  118. /**
  119. * Return array of widgets disabled for selection
  120. *
  121. * @return string[]
  122. */
  123. protected function _getSkippedWidgets()
  124. {
  125. return $this->_coreRegistry->registry('skip_widgets');
  126. }
  127. }