/** * 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(); }
/** * @param string $name * @param string $type * @param int $preConfigId * @param array $params * @param array $preconfigure * @param string $widgetXml * @param \Magento\Widget\Block\BlockInterface|null $widgetBlock * @return void * @dataProvider generateWidgetDataProvider */ protected function generalForGenerateWidget($name, $type, $preConfigId, $params, $preconfigure, $widgetXml, $widgetBlock) { $this->widgetResourceMock->expects($this->any())->method('loadPreconfiguredWidget')->with($preConfigId)->willReturn($preconfigure); $this->widgetMock->expects($this->any())->method('getWidgetByClassType')->with($type)->willReturn($widgetXml); $this->layoutMock->expects($this->any())->method('createBlock')->with($type, $name, ['data' => $params])->willReturn($widgetBlock); }