Example #1
0
 /**
  * General method for generate widget
  *
  * @param string[] $construction
  * @return string
  */
 public function generateWidget($construction)
 {
     $params = $this->getParameters($construction[2]);
     // Determine what name block should have in layout
     $name = null;
     if (isset($params['name'])) {
         $name = $params['name'];
     }
     // validate required parameter type or id
     if (!empty($params['type'])) {
         $type = $params['type'];
     } elseif (!empty($params['id'])) {
         $preConfigured = $this->_widgetResource->loadPreconfiguredWidget($params['id']);
         $type = $preConfigured['widget_type'];
         $params = $preConfigured['parameters'];
     } else {
         return '';
     }
     // we have no other way to avoid fatal errors for type like 'cms/widget__link', '_cms/widget_link' etc.
     $xml = $this->_widget->getWidgetByClassType($type);
     if ($xml === null) {
         return '';
     }
     // define widget block and check the type is instance of Widget Interface
     $widget = $this->_layout->createBlock($type, $name, ['data' => $params]);
     if (!$widget instanceof \Magento\Widget\Block\BlockInterface) {
         return '';
     }
     return $widget->toHtml();
 }