Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $_assetRepo;
17 
21  protected $_widget;
22 
26  protected $_backendUrl;
27 
31  protected $urlDecoder;
32 
36  protected $_widgetFactory;
37 
41  protected $urlEncoder;
42 
46  private $registry;
47 
56  public function __construct(
57  \Magento\Backend\Model\UrlInterface $backendUrl,
58  \Magento\Framework\Url\DecoderInterface $urlDecoder,
59  \Magento\Framework\View\Asset\Repository $assetRepo,
60  \Magento\Widget\Model\WidgetFactory $widgetFactory,
61  \Magento\Framework\Url\EncoderInterface $urlEncoder,
62  \Magento\Framework\Registry $registry
63  ) {
64  $this->_backendUrl = $backendUrl;
65  $this->urlDecoder = $urlDecoder;
66  $this->_assetRepo = $assetRepo;
67  $this->_widgetFactory = $widgetFactory;
68  $this->urlEncoder = $urlEncoder;
69  $this->registry = $registry;
70  }
71 
78  public function getConfig(\Magento\Framework\DataObject $config) : \Magento\Framework\DataObject
79  {
80  $settings = $this->getPluginSettings($config);
81  return $config->addData($settings);
82  }
83 
90  public function getPluginSettings($config)
91  {
92  $widgetWysiwyg = [
93  [
94  'name' => 'magentowidget',
95  'src' => $this->getWysiwygJsPluginSrc(),
96  'options' => [
97  'window_url' => $this->getWidgetWindowUrl($config),
98  'types' => $this->getAvailableWidgets($config),
99  'error_image_url' => $this->getErrorImageUrl(),
100  'placeholders' => $this->getWidgetPlaceholderImageUrls(),
101  ],
102  ]
103  ];
104 
105  $configPlugins = (array) $config->getData('plugins');
106 
107  $widgetConfig['plugins'] = array_merge($configPlugins, $widgetWysiwyg);
108  return $widgetConfig;
109  }
110 
117  {
118  return $this->_widgetFactory->create()->getPlaceholderImageUrls();
119  }
120 
125  public function getErrorImageUrl()
126  {
127  return $this->_assetRepo->getUrl('Magento_Widget::error.png');
128  }
129 
134  public function getWysiwygJsPluginSrc()
135  {
136  return $this->_assetRepo->getUrl('mage/adminhtml/wysiwyg/tiny_mce/plugins/magentowidget/editor_plugin.js');
137  }
138 
145  public function getWidgetWindowUrl($config)
146  {
147  $params = [];
148 
149  $skipped = is_array($config->getData('skip_widgets')) ? $config->getData('skip_widgets') : [];
150  if ($config->hasData('widget_filters')) {
151  $all = $this->_widgetFactory->create()->getWidgets();
152  $filtered = $this->_widgetFactory->create()->getWidgets($config->getData('widget_filters'));
153  foreach ($all as $code => $widget) {
154  if (!isset($filtered[$code])) {
155  $skipped[] = $widget['@']['type'];
156  }
157  }
158  }
159 
160  if (count($skipped) > 0) {
161  $params['skip_widgets'] = $this->encodeWidgetsToQuery($skipped);
162  }
163  return $this->_backendUrl->getUrl('adminhtml/widget/index', $params);
164  }
165 
172  public function encodeWidgetsToQuery($widgets)
173  {
174  $widgets = is_array($widgets) ? $widgets : [$widgets];
175  $param = implode(',', $widgets);
176  return $this->urlEncoder->encode($param);
177  }
178 
185  public function decodeWidgetsFromQuery($queryParam)
186  {
187  $param = $this->urlDecoder->decode($queryParam);
188  return preg_split('/\s*\,\s*/', $param, 0, PREG_SPLIT_NO_EMPTY);
189  }
190 
195  public function getAvailableWidgets($config)
196  {
197  if (!$config->hasData('widget_types')) {
198  $result = [];
199  $allWidgets = $this->_widgetFactory->create()->getWidgetsArray();
200  $skipped = $this->_getSkippedWidgets();
201  foreach ($allWidgets as $widget) {
202  if (is_array($skipped) && in_array($widget['type'], $skipped)) {
203  continue;
204  }
205  $result[] = $widget['name']->getText();
206  }
207  }
208 
209  return $result;
210  }
211 
217  protected function _getSkippedWidgets()
218  {
219  return $this->registry->registry('skip_widgets');
220  }
221 }
$config
Definition: fraud_order.php:17
__construct(\Magento\Backend\Model\UrlInterface $backendUrl, \Magento\Framework\Url\DecoderInterface $urlDecoder, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Widget\Model\WidgetFactory $widgetFactory, \Magento\Framework\Url\EncoderInterface $urlEncoder, \Magento\Framework\Registry $registry)
Definition: Config.php:56
getConfig(\Magento\Framework\DataObject $config)
Definition: Config.php:78
$settings
Definition: bootstrap.php:29
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$code
Definition: info.phtml:12