/** * Return widget presentation code in WYSIWYG editor * * @param string $type Widget Type * @param array $params Pre-configured Widget Params * @param bool $asIs Return result as widget directive(true) or as placeholder image(false) * @return string Widget directive ready to parse * @api */ public function getWidgetDeclaration($type, $params = [], $asIs = true) { $directive = '{{widget type="' . $type . '"'; foreach ($params as $name => $value) { // Retrieve default option value if pre-configured if ($name == 'conditions') { $name = 'conditions_encoded'; $value = $this->conditionsHelper->encode($value); } elseif (is_array($value)) { $value = implode(',', $value); } elseif (trim($value) == '') { $widget = $this->getConfigAsObject($type); $parameters = $widget->getParameters(); if (isset($parameters[$name]) && is_object($parameters[$name])) { $value = $parameters[$name]->getValue(); } } if ($value) { $directive .= sprintf(' %s="%s"', $name, $value); } } $directive .= '}}'; if ($asIs) { return $directive; } $html = sprintf('<img id="%s" src="%s" title="%s">', $this->idEncode($directive), $this->getPlaceholderImageUrl($type), $this->escaper->escapeUrl($directive)); return $html; }
/** * @return \Magento\Rule\Model\Condition\Combine */ protected function getConditions() { $conditions = $this->getData('conditions_encoded') ? $this->getData('conditions_encoded') : $this->getData('conditions'); if ($conditions) { $conditions = $this->conditionsHelper->decode($conditions); } $this->rule->loadPost(['conditions' => $conditions]); return $this->rule->getConditions(); }
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); }
/** * Generate layout update xml * * @param string $container * @param string $templatePath * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function generateLayoutUpdateXml($container, $templatePath = '') { $templateFilename = $this->_viewFileSystem->getTemplateFileName($templatePath, ['area' => $this->getArea(), 'themeId' => $this->getThemeId(), 'module' => \Magento\Framework\View\Element\AbstractBlock::extractModuleName($this->getType())]); if (!$this->getId() && !$this->isCompleteToCreate() || $templatePath && !is_readable($templateFilename)) { return ''; } $parameters = $this->getWidgetParameters(); $xml = '<body><referenceContainer name="' . $container . '">'; $template = ''; if (isset($parameters['template'])) { unset($parameters['template']); } if ($templatePath) { $template = ' template="' . $templatePath . '"'; } $hash = $this->mathRandom->getUniqueHash(); $xml .= '<block class="' . $this->getType() . '" name="' . $hash . '"' . $template . '>'; foreach ($parameters as $name => $value) { if ($name == 'conditions') { $name = 'conditions_encoded'; $value = $this->conditionsHelper->encode($value); } elseif (is_array($value)) { $value = implode(',', $value); } if ($name && strlen((string) $value)) { $xml .= '<action method="setData">' . '<argument name="name" xsi:type="string">' . $name . '</argument>' . '<argument name="value" xsi:type="string">' . $this->_escaper->escapeHtml($value) . '</argument>' . '</action>'; } } $xml .= '</block></referenceContainer></body>'; return $xml; }
public function testEncodeDecode() { $value = ['1' => ["type" => "Magento\\CatalogWidget\\Model\\Rule\\Condition\\Combine", "aggregator" => "all", "value" => "1", "new_child" => ""], '1--1' => ["type" => "Magento\\CatalogWidget\\Model\\Rule\\Condition\\Product", "attribute" => "attribute_set_id", "value" => "4", "operator" => "=="], '1--2' => ["type" => "Magento\\CatalogWidget\\Model\\Rule\\Condition\\Product", "attribute" => "category_ids", "value" => "2", "operator" => "=="]]; $encoded = $this->conditions->encode($value); $this->assertEquals($value, $this->conditions->decode($encoded)); }