/** * 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 testGetConfigAsObjectWidgetNoFound() { $this->dataStorageMock->expects($this->once())->method('get')->willReturn([]); $resultObject = $this->widget->getConfigAsObject('Magento\\Cms\\Block\\Widget\\Page\\Link'); $this->assertInstanceOf('Magento\\Framework\\DataObject', $resultObject); $this->assertSame([], $resultObject->getData()); }
/** * Format widget pseudo-code for inserting into wysiwyg editor * * @return void */ public function execute() { $type = $this->getRequest()->getPost('widget_type'); $params = $this->getRequest()->getPost('parameters', array()); $asIs = $this->getRequest()->getPost('as_is'); $html = $this->_widget->getWidgetDeclaration($type, $params, $asIs); $this->getResponse()->setBody($html); }
/** * @param string $type * @param string $expectedFile * @return string * * @dataProvider getPlaceholderImageUrlDataProvider * @magentoAppIsolation enabled */ public function testGetPlaceholderImageUrl($type, $expectedFile) { $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE); $objectManager->get('Magento\\Framework\\View\\DesignInterface')->setDesignTheme('Magento/backend'); $expectedFilePath = "/adminhtml/Magento/backend/en_US/{$expectedFile}"; $url = $this->_model->getPlaceholderImageUrl($type); $this->assertStringEndsWith($expectedFilePath, $url); }
/** * 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; }
/** * Add fields to main fieldset based on specified widget type * * @throws \Magento\Framework\Model\Exception * @return $this */ public function addFields() { // get configuration node and translation helper if (!$this->getWidgetType()) { throw new \Magento\Framework\Model\Exception(__('Please specify a Widget Type.')); } $config = $this->_widget->getConfigAsObject($this->getWidgetType()); if (!$config->getParameters()) { return $this; } foreach ($config->getParameters() as $parameter) { $this->_addField($parameter); } return $this; }
public function testGetWidgetDeclaration() { $mathRandomMock = $this->getMock('\\Magento\\Framework\\Math\\Random', ['getRandomString'], [], '', false); $mathRandomMock->expects($this->any())->method('getRandomString')->willReturn('asdf'); $reflection = new \ReflectionClass(get_class($this->widget)); $reflectionProperty = $reflection->getProperty('mathRandom'); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($this->widget, $mathRandomMock); $conditions = [['type' => 'Magento\\CatalogWidget\\Model\\Rule\\Condition\\Combine', 'aggregator' => 'all', 'value' => '1', 'new_child' => '']]; $params = ['title' => 'my widget', 'show_pager' => '1', 'products_per_page' => '5', 'products_count' => '10', 'template' => 'product/widget/content/grid.phtml', 'conditions' => $conditions]; $this->conditionsHelper->expects($this->once())->method('encode')->with($conditions)->willReturn('encoded-conditions-string'); $result = $this->widget->getWidgetDeclaration('Magento\\CatalogWidget\\Block\\Product\\ProductsList', $params); $this->assertContains('{{widget type="Magento\\CatalogWidget\\Block\\Product\\ProductsList"', $result); $this->assertContains('conditions_encoded="encoded-conditions-string"', $result); $this->assertContains('page_var_name="pasdf"}}', $result); }
/** * @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); }
public function testGetWidgetDeclarationTypeWithBackslashes() { $this->assertContains('Magento\\\\Widget\\\\Backslashed\\\\ClassName', $this->_model->getWidgetDeclaration('Magento\\Widget\\Backslashed\\ClassName')); }
public function getTabsArray($filters = []) { return parent::getWidgetsArray($filters); }