Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Form.php
Go to the documentation of this file.
1 <?php
13 
14 class Form extends \Magento\Backend\Block\Widget\Form\Generic
15 {
19  protected $_widgetFactory;
20 
28  public function __construct(
29  \Magento\Backend\Block\Template\Context $context,
30  \Magento\Framework\Registry $registry,
31  \Magento\Framework\Data\FormFactory $formFactory,
32  \Magento\Widget\Model\WidgetFactory $widgetFactory,
33  array $data = []
34  ) {
35  $this->_widgetFactory = $widgetFactory;
36  parent::__construct($context, $registry, $formFactory, $data);
37  }
38 
44  protected function _prepareForm()
45  {
47  $form = $this->_formFactory->create();
48 
49  // Add messages container to fieldset
50  $fieldset = $form->addFieldset('base_fieldset', ['legend' => '<div data-role="messages"></div>',
51  'comment' => __('Inserting a widget does not create a widget instance.')]);
52 
53  $fieldset->addField(
54  'select_widget_type',
55  'select',
56  [
57  'label' => __('Widget Type'),
58  'title' => __('Widget Type'),
59  'name' => 'widget_type',
60  'required' => true,
61  'onchange' => "wWidget.validateField()",
62  'options' => $this->_getWidgetSelectOptions(),
63  'after_element_html' => $this->_getWidgetSelectAfterHtml()
64  ]
65  );
66 
67  $form->setUseContainer(true);
68  $form->setId('widget_options_form');
69  $form->setMethod('post');
70  $form->setAction($this->getUrl('adminhtml/*/buildWidget'));
71  $this->setForm($form);
72  }
73 
79  protected function _getWidgetSelectOptions()
80  {
81  foreach ($this->_getAvailableWidgets(true) as $data) {
82  $options[$data['type']] = $data['name'];
83  }
84  return $options;
85  }
86 
92  protected function _getWidgetSelectAfterHtml()
93  {
94  $html = '<p class="nm"><small></small></p>';
95  $i = 0;
96  foreach ($this->_getAvailableWidgets(true) as $data) {
97  $html .= sprintf('<div id="widget-description-%s" class="no-display">%s</div>', $i, $data['description']);
98  $i++;
99  }
100  return $html;
101  }
102 
109  protected function _getAvailableWidgets($withEmptyElement = false)
110  {
111  if (!$this->hasData('available_widgets')) {
112  $result = [];
113  $allWidgets = $this->_widgetFactory->create()->getWidgetsArray();
114  $skipped = $this->_getSkippedWidgets();
115  foreach ($allWidgets as $widget) {
116  if (is_array($skipped) && in_array($widget['type'], $skipped)) {
117  continue;
118  }
119  $result[] = $widget;
120  }
121  if ($withEmptyElement) {
122  array_unshift($result, ['type' => '', 'name' => __('-- Please Select --'), 'description' => '']);
123  }
124  $this->setData('available_widgets', $result);
125  }
126 
127  return $this->_getData('available_widgets');
128  }
129 
135  protected function _getSkippedWidgets()
136  {
137  return $this->_coreRegistry->registry('skip_widgets');
138  }
139 }
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Widget\Model\WidgetFactory $widgetFactory, array $data=[])
Definition: Form.php:28
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
_getAvailableWidgets($withEmptyElement=false)
Definition: Form.php:109
__()
Definition: __.php:13
setData($key, $value=null)
Definition: DataObject.php:72
$i
Definition: gallery.phtml:31