Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Widget.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Widget\Model;
7 
15 class Widget
16 {
20  protected $dataStorage;
21 
25  protected $configCacheType;
26 
30  protected $assetRepo;
31 
35  protected $assetSource;
36 
40  protected $viewFileSystem;
41 
45  protected $escaper;
46 
50  protected $widgetsArray = [];
51 
55  protected $conditionsHelper;
56 
60  private $mathRandom;
61 
70  public function __construct(
71  \Magento\Framework\Escaper $escaper,
72  \Magento\Widget\Model\Config\Data $dataStorage,
73  \Magento\Framework\View\Asset\Repository $assetRepo,
74  \Magento\Framework\View\Asset\Source $assetSource,
75  \Magento\Framework\View\FileSystem $viewFileSystem,
76  \Magento\Widget\Helper\Conditions $conditionsHelper
77  ) {
78  $this->escaper = $escaper;
79  $this->dataStorage = $dataStorage;
80  $this->assetRepo = $assetRepo;
81  $this->assetSource = $assetSource;
82  $this->viewFileSystem = $viewFileSystem;
83  $this->conditionsHelper = $conditionsHelper;
84  }
85 
91  private function getMathRandom()
92  {
93  if ($this->mathRandom === null) {
95  ->get(\Magento\Framework\Math\Random::class);
96  }
97  return $this->mathRandom;
98  }
99 
106  public function getWidgetByClassType($type)
107  {
108  $widgets = $this->getWidgets();
110  foreach ($widgets as $widget) {
111  if (isset($widget['@'])) {
112  if (isset($widget['@']['type'])) {
113  if ($type === $widget['@']['type']) {
114  return $widget;
115  }
116  }
117  }
118  }
119  return null;
120  }
121 
130  public function getConfigAsXml($type)
131  {
132  return $this->getXmlElementByType($type);
133  }
134 
141  public function getConfigAsObject($type)
142  {
143  $widget = $this->getWidgetByClassType($type);
144 
145  $object = new \Magento\Framework\DataObject();
146  if ($widget === null) {
147  return $object;
148  }
149  $widget = $this->getAsCanonicalArray($widget);
150 
151  // Save all nodes to object data
152  $object->setType($type);
153  $object->setData($widget);
154 
155  // Correct widget parameters and convert its data to objects
156  $newParams = $this->prepareWidgetParameters($object);
157  $object->setData('parameters', $newParams);
158 
159  return $object;
160  }
161 
168  protected function prepareWidgetParameters(\Magento\Framework\DataObject $object)
169  {
170  $params = $object->getData('parameters');
171  $newParams = [];
172  if (is_array($params)) {
173  $sortOrder = 0;
174  foreach ($params as $key => $data) {
175  if (is_array($data)) {
176  $data = $this->prepareDropDownValues($data, $key, $sortOrder);
177  $data = $this->prepareHelperBlock($data);
178 
179  $newParams[$key] = new \Magento\Framework\DataObject($data);
180  $sortOrder++;
181  }
182  }
183  }
184  uasort($newParams, [$this, 'sortParameters']);
185 
186  return $newParams;
187  }
188 
197  protected function prepareDropDownValues(array $data, $key, $sortOrder)
198  {
199  $data['key'] = $key;
200  $data['sort_order'] = isset($data['sort_order']) ? (int)$data['sort_order'] : $sortOrder;
201 
202  $values = [];
203  if (isset($data['values']) && is_array($data['values'])) {
204  foreach ($data['values'] as $value) {
205  if (isset($value['label']) && isset($value['value'])) {
206  $values[] = $value;
207  }
208  }
209  }
210  $data['values'] = $values;
211 
212  return $data;
213  }
214 
221  protected function prepareHelperBlock(array $data)
222  {
223  if (isset($data['helper_block'])) {
224  $helper = new \Magento\Framework\DataObject();
225  if (isset($data['helper_block']['data']) && is_array($data['helper_block']['data'])) {
226  $helper->addData($data['helper_block']['data']);
227  }
228  if (isset($data['helper_block']['type'])) {
229  $helper->setType($data['helper_block']['type']);
230  }
231  $data['helper_block'] = $helper;
232  }
233 
234  return $data;
235  }
236 
243  public function getWidgets($filters = [])
244  {
245  $widgets = $this->dataStorage->get();
246  $result = $widgets;
247 
248  // filter widgets by params
249  if (is_array($filters) && count($filters) > 0) {
250  foreach ($widgets as $code => $widget) {
251  try {
252  foreach ($filters as $field => $value) {
253  if (!isset($widget[$field]) || (string)$widget[$field] != $value) {
254  throw new \Exception();
255  }
256  }
257  } catch (\Exception $e) {
258  unset($result[$code]);
259  continue;
260  }
261  }
262  }
263 
264  return $result;
265  }
266 
273  public function getWidgetsArray($filters = [])
274  {
275  if (empty($this->widgetsArray)) {
276  $result = [];
277  foreach ($this->getWidgets($filters) as $code => $widget) {
278  $result[$widget['name']] = [
279  'name' => __((string)$widget['name']),
280  'code' => $code,
281  'type' => $widget['@']['type'],
282  'description' => __((string)$widget['description']),
283  ];
284  }
285  usort($result, [$this, "sortWidgets"]);
286  $this->widgetsArray = $result;
287  }
288  return $this->widgetsArray;
289  }
290 
299  public function getWidgetDeclaration($type, $params = [], $asIs = true)
300  {
301  $directive = '{{widget type="' . $type . '"';
302  $widget = $this->getConfigAsObject($type);
303 
304  foreach ($params as $name => $value) {
305  // Retrieve default option value if pre-configured
306  if ($name == 'conditions') {
307  $name = 'conditions_encoded';
308  $value = $this->conditionsHelper->encode($value);
309  } elseif (is_array($value)) {
310  $value = implode(',', $value);
311  } elseif (trim($value) == '') {
312  $parameters = $widget->getParameters();
313  if (isset($parameters[$name]) && is_object($parameters[$name])) {
314  $value = $parameters[$name]->getValue();
315  }
316  }
317  if (isset($value)) {
318  $directive .= sprintf(' %s="%s"', $name, $this->escaper->escapeQuote($value));
319  }
320  }
321 
322  $directive .= $this->getWidgetPageVarName($params);
323 
324  $directive .= sprintf(' type_name="%s"', $widget['name']);
325 
326  $directive .= '}}';
327 
328  if ($asIs) {
329  return $directive;
330  }
331 
332  $html = sprintf(
333  '<img id="%s" src="%s" title="%s">',
334  $this->idEncode($directive),
336  $this->escaper->escapeUrl($directive)
337  );
338  return $html;
339  }
340 
346  private function getWidgetPageVarName($params = [])
347  {
348  $pageVarName = '';
349  if (array_key_exists('show_pager', $params) && (bool)$params['show_pager']) {
350  $pageVarName = sprintf(
351  ' %s="%s"',
352  'page_var_name',
353  'p' . $this->getMathRandom()->getRandomString(5, \Magento\Framework\Math\Random::CHARS_LOWERS)
354  );
355  }
356  return $pageVarName;
357  }
358 
365  public function getPlaceholderImageUrl($type)
366  {
367  $placeholder = false;
368  $widget = $this->getWidgetByClassType($type);
369  if (is_array($widget) && isset($widget['placeholder_image'])) {
370  $placeholder = (string)$widget['placeholder_image'];
371  }
372  if ($placeholder) {
373  $asset = $this->assetRepo->createAsset($placeholder);
374  $placeholder = $this->assetSource->getFile($asset);
375  if ($placeholder) {
376  return $asset->getUrl();
377  }
378  }
379  return $this->assetRepo->getUrl('Magento_Widget::placeholder.png');
380  }
381 
389  public function getPlaceholderImageUrls()
390  {
391  $result = [];
392  $widgets = $this->getWidgets();
394  foreach ($widgets as $widget) {
395  if (isset($widget['@'])) {
396  if (isset($widget['@']['type'])) {
397  $type = $widget['@']['type'];
399  }
400  }
401  }
402  return $result;
403  }
404 
411  protected function getAsCanonicalArray($inputArray)
412  {
413  if (array_key_exists('@', $inputArray)) {
414  unset($inputArray['@']);
415  }
416  foreach ($inputArray as $key => $value) {
417  if (!is_array($value)) {
418  continue;
419  }
420  $inputArray[$key] = $this->getAsCanonicalArray($value);
421  }
422  return $inputArray;
423  }
424 
431  protected function idEncode($string)
432  {
433  return strtr(base64_encode($string), '+/=', ':_-');
434  }
435 
443  protected function sortWidgets($firstElement, $secondElement)
444  {
445  return strcmp($firstElement["name"], $secondElement["name"]);
446  }
447 
455  protected function sortParameters($firstElement, $secondElement)
456  {
457  $aOrder = (int)$firstElement->getData('sort_order');
458  $bOrder = (int)$secondElement->getData('sort_order');
459  return $aOrder < $bOrder ? -1 : ($aOrder > $bOrder ? 1 : 0);
460  }
461 }
$helper
Definition: iframe.phtml:13
getWidgets($filters=[])
Definition: Widget.php:243
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
sortWidgets($firstElement, $secondElement)
Definition: Widget.php:443
$values
Definition: options.phtml:88
__()
Definition: __.php:13
prepareDropDownValues(array $data, $key, $sortOrder)
Definition: Widget.php:197
getWidgetsArray($filters=[])
Definition: Widget.php:273
$type
Definition: item.phtml:13
__construct(\Magento\Framework\Escaper $escaper, \Magento\Widget\Model\Config\Data $dataStorage, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\View\Asset\Source $assetSource, \Magento\Framework\View\FileSystem $viewFileSystem, \Magento\Widget\Helper\Conditions $conditionsHelper)
Definition: Widget.php:70
$value
Definition: gender.phtml:16
$filters
Definition: uploader.phtml:11
getAsCanonicalArray($inputArray)
Definition: Widget.php:411
prepareHelperBlock(array $data)
Definition: Widget.php:221
getWidgetDeclaration($type, $params=[], $asIs=true)
Definition: Widget.php:299
prepareWidgetParameters(\Magento\Framework\DataObject $object)
Definition: Widget.php:168
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
sortParameters($firstElement, $secondElement)
Definition: Widget.php:455
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14