/** * Generate widget * * @param string[] $construction * @return string */ public function widgetDirective($construction) { $params = $this->_getIncludeParameters($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(); }
public function testGetWidgetByClassType() { $widgetOne = ['@' => ['type' => 'type1']]; $widgets = ['widget1' => $widgetOne]; $this->dataStorageMock->expects($this->any())->method('get')->willReturn($widgets); $this->assertEquals($widgetOne, $this->widget->getWidgetByClassType('type1')); $this->assertNull($this->widget->getWidgetByClassType('type2')); }
public function testGetWidgetByClassType() { $widgetOne = array('@' => array('type' => 'type1')); $widgets = array('widget1' => $widgetOne); $this->_storage->expects($this->any())->method('get')->will($this->returnValue($widgets)); $this->assertEquals($widgetOne, $this->_model->getWidgetByClassType('type1')); $this->assertNull($this->_model->getWidgetByClassType('type2')); }
/** * Load widget XML config and merge with theme widget config * * @return array|null * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getWidgetConfigAsArray() { if ($this->_widgetConfigXml === null) { $this->_widgetConfigXml = $this->_widgetModel->getWidgetByClassType($this->getType()); if ($this->_widgetConfigXml) { $configFile = $this->_viewFileSystem->getFilename('widget.xml', ['area' => $this->getArea(), 'theme' => $this->getThemeId(), 'module' => $this->_namespaceResolver->determineOmittedNamespace(preg_replace('/^(.+?)\\/.+$/', '\\1', $this->getType()), true)]); $isReadable = $configFile && $this->_directory->isReadable($this->_directory->getRelativePath($configFile)); if ($isReadable) { $config = $this->_reader->readFile($configFile); $widgetName = isset($this->_widgetConfigXml['name']) ? $this->_widgetConfigXml['name'] : null; $themeWidgetConfig = null; if ($widgetName !== null) { foreach ($config as $widget) { if (isset($widget['name']) && $widgetName === $widget['name']) { $themeWidgetConfig = $widget; break; } } } if ($themeWidgetConfig) { $this->_widgetConfigXml = array_replace_recursive($this->_widgetConfigXml, $themeWidgetConfig); } } } } return $this->_widgetConfigXml; }