Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Widget Class Reference

Public Member Functions

 __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)
 
 getConfigAsXml ($type)
 
 getConfigAsObject ($type)
 
 getWidgets ($filters=[])
 
 getWidgetsArray ($filters=[])
 
 getWidgetDeclaration ($type, $params=[], $asIs=true)
 
 getPlaceholderImageUrl ($type)
 

Protected Member Functions

 prepareWidgetParameters (\Magento\Framework\DataObject $object)
 
 prepareDropDownValues (array $data, $key, $sortOrder)
 
 prepareHelperBlock (array $data)
 
 getAsCanonicalArray ($inputArray)
 
 idEncode ($string)
 
 sortWidgets ($firstElement, $secondElement)
 
 sortParameters ($firstElement, $secondElement)
 

Protected Attributes

 $dataStorage
 
 $configCacheType
 
 $assetRepo
 
 $assetSource
 
 $viewFileSystem
 
 $escaper
 
 $widgetsArray = []
 
 $conditionsHelper
 

Detailed Description

Widget model for different purposes @SuppressWarnings(PHPMD.CouplingBetweenObjects)

@api

Since
100.0.2

Definition at line 15 of file Widget.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\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 at line 70 of file Widget.php.

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  }

Member Function Documentation

◆ getAsCanonicalArray()

getAsCanonicalArray (   $inputArray)
protected

Remove attributes from widget array and emulate work of \Magento\Framework\Simplexml\Element::asCanonicalArray

Parameters
array$inputArray
Returns
array

Definition at line 411 of file Widget.php.

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  }
$value
Definition: gender.phtml:16
getAsCanonicalArray($inputArray)
Definition: Widget.php:411

◆ getConfigAsObject()

getConfigAsObject (   $type)

Return widget XML configuration as \Magento\Framework\DataObject and makes some data preparations

Parameters
string$typeWidget type
Returns
\Magento\Framework\DataObject

Definition at line 141 of file Widget.php.

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  }
$type
Definition: item.phtml:13
getAsCanonicalArray($inputArray)
Definition: Widget.php:411
prepareWidgetParameters(\Magento\Framework\DataObject $object)
Definition: Widget.php:168

◆ getConfigAsXml()

getConfigAsXml (   $type)

Return widget XML configuration as \Magento\Framework\DataObject and makes some data preparations

Parameters
string$typeWidget type
Returns
null|\Magento\Framework\Simplexml\Element
Deprecated:
101.0.0

Definition at line 130 of file Widget.php.

131  {
132  return $this->getXmlElementByType($type);
133  }
$type
Definition: item.phtml:13

◆ getPlaceholderImageUrl()

getPlaceholderImageUrl (   $type)

Get image URL of WYSIWYG placeholder image

Parameters
string$type
Returns
string

Definition at line 365 of file Widget.php.

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  }
$type
Definition: item.phtml:13

◆ getWidgetDeclaration()

getWidgetDeclaration (   $type,
  $params = [],
  $asIs = true 
)

Return widget presentation code in WYSIWYG editor

Parameters
string$typeWidget Type
array$paramsPre-configured Widget Params
bool$asIsReturn result as widget directive(true) or as placeholder image(false)
Returns
string Widget directive ready to parse

Definition at line 299 of file Widget.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ getWidgets()

getWidgets (   $filters = [])

Return filtered list of widgets

Parameters
array$filtersKey-value array of filters for widget node properties
Returns
array

Definition at line 243 of file Widget.php.

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  }
$value
Definition: gender.phtml:16
$filters
Definition: uploader.phtml:11
$code
Definition: info.phtml:12

◆ getWidgetsArray()

getWidgetsArray (   $filters = [])

Return list of widgets as array

Parameters
array$filtersKey-value array of filters for widget node properties
Returns
array

Definition at line 273 of file Widget.php.

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  }
getWidgets($filters=[])
Definition: Widget.php:243
__()
Definition: __.php:13
$filters
Definition: uploader.phtml:11
$code
Definition: info.phtml:12

◆ idEncode()

idEncode (   $string)
protected

Encode string to valid HTML id element, based on base64 encoding

Parameters
string$string
Returns
string

Definition at line 431 of file Widget.php.

432  {
433  return strtr(base64_encode($string), '+/=', ':_-');
434  }

◆ prepareDropDownValues()

prepareDropDownValues ( array  $data,
  $key,
  $sortOrder 
)
protected

Prepare drop-down values

Parameters
array$data
string$key
int$sortOrder
Returns
array

Definition at line 197 of file Widget.php.

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  }
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16

◆ prepareHelperBlock()

prepareHelperBlock ( array  $data)
protected

Prepare helper block

Parameters
array$data
Returns
array

Definition at line 221 of file Widget.php.

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  }
$helper
Definition: iframe.phtml:13

◆ prepareWidgetParameters()

prepareWidgetParameters ( \Magento\Framework\DataObject  $object)
protected

Prepare widget parameters

Parameters
\Magento\Framework\DataObject$object
Returns
array

Definition at line 168 of file Widget.php.

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  }
prepareDropDownValues(array $data, $key, $sortOrder)
Definition: Widget.php:197
prepareHelperBlock(array $data)
Definition: Widget.php:221
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ sortParameters()

sortParameters (   $firstElement,
  $secondElement 
)
protected

Widget parameters sort callback

Parameters
\Magento\Framework\DataObject$firstElement
\Magento\Framework\DataObject$secondElement
Returns
int

Definition at line 455 of file Widget.php.

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  }

◆ sortWidgets()

sortWidgets (   $firstElement,
  $secondElement 
)
protected

User-defined widgets sorting by Name

Parameters
array$firstElement
array$secondElement
Returns
bool

Definition at line 443 of file Widget.php.

444  {
445  return strcmp($firstElement["name"], $secondElement["name"]);
446  }

Field Documentation

◆ $assetRepo

$assetRepo
protected

Definition at line 30 of file Widget.php.

◆ $assetSource

$assetSource
protected

Definition at line 35 of file Widget.php.

◆ $conditionsHelper

$conditionsHelper
protected

Definition at line 55 of file Widget.php.

◆ $configCacheType

$configCacheType
protected

Definition at line 25 of file Widget.php.

◆ $dataStorage

$dataStorage
protected

Definition at line 20 of file Widget.php.

◆ $escaper

$escaper
protected

Definition at line 45 of file Widget.php.

◆ $viewFileSystem

$viewFileSystem
protected

Definition at line 40 of file Widget.php.

◆ $widgetsArray

$widgetsArray = []
protected

Definition at line 50 of file Widget.php.


The documentation for this class was generated from the following file: