Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Output.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 namespace Magento\Catalog\Helper;
9 
13 
15 {
21  protected $_handlers;
22 
28  protected $_templateProcessor = null;
29 
35  protected $_catalogData = null;
36 
42  protected $_eavConfig;
43 
47  protected $_escaper;
48 
52  private $directivePatterns;
53 
62  public function __construct(
63  \Magento\Framework\App\Helper\Context $context,
64  \Magento\Eav\Model\Config $eavConfig,
65  Data $catalogData,
66  \Magento\Framework\Escaper $escaper,
67  $directivePatterns = []
68  ) {
69  $this->_eavConfig = $eavConfig;
70  $this->_catalogData = $catalogData;
71  $this->_escaper = $escaper;
72  $this->directivePatterns = $directivePatterns;
73  parent::__construct($context);
74  }
75 
79  protected function _getTemplateProcessor()
80  {
81  if (null === $this->_templateProcessor) {
82  $this->_templateProcessor = $this->_catalogData->getPageTemplateProcessor();
83  }
84 
86  }
87 
95  public function addHandler($method, $handler)
96  {
97  if (!is_object($handler)) {
98  return $this;
99  }
100  $method = strtolower($method);
101 
102  if (!isset($this->_handlers[$method])) {
103  $this->_handlers[$method] = [];
104  }
105 
106  $this->_handlers[$method][] = $handler;
107  return $this;
108  }
109 
116  public function getHandlers($method)
117  {
118  $method = strtolower($method);
119  return $this->_handlers[$method] ?? [];
120  }
121 
130  public function process($method, $result, $params)
131  {
132  foreach ($this->getHandlers($method) as $handler) {
133  if (method_exists($handler, $method)) {
134  $result = $handler->{$method}($this, $result, $params);
135  }
136  }
137  return $result;
138  }
139 
150  public function productAttribute($product, $attributeHtml, $attributeName)
151  {
152  $attribute = $this->_eavConfig->getAttribute(ModelProduct::ENTITY, $attributeName);
153  if ($attribute &&
154  $attribute->getId() &&
155  $attribute->getFrontendInput() != 'media_image' &&
156  (!$attribute->getIsHtmlAllowedOnFront() &&
157  !$attribute->getIsWysiwygEnabled())
158  ) {
159  if ($attribute->getFrontendInput() != 'price') {
160  $attributeHtml = $this->_escaper->escapeHtml($attributeHtml);
161  }
162  if ($attribute->getFrontendInput() == 'textarea') {
163  $attributeHtml = nl2br($attributeHtml);
164  }
165  }
166  if ($attributeHtml !== null
167  && $attribute->getIsHtmlAllowedOnFront()
168  && $attribute->getIsWysiwygEnabled()
169  && $this->isDirectivesExists($attributeHtml)
170  ) {
171  $attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
172  }
173 
174  $attributeHtml = $this->process(
175  'productAttribute',
176  $attributeHtml,
177  ['product' => $product, 'attribute' => $attributeName]
178  );
179 
180  return $attributeHtml;
181  }
182 
192  public function categoryAttribute($category, $attributeHtml, $attributeName)
193  {
194  $attribute = $this->_eavConfig->getAttribute(ModelCategory::ENTITY, $attributeName);
195 
196  if ($attribute &&
197  $attribute->getFrontendInput() != 'image' &&
198  (!$attribute->getIsHtmlAllowedOnFront() &&
199  !$attribute->getIsWysiwygEnabled())
200  ) {
201  $attributeHtml = $this->_escaper->escapeHtml($attributeHtml);
202  }
203  if ($attributeHtml !== null
204  && $attribute->getIsHtmlAllowedOnFront()
205  && $attribute->getIsWysiwygEnabled()
206  && $this->isDirectivesExists($attributeHtml)
207 
208  ) {
209  $attributeHtml = $this->_getTemplateProcessor()->filter($attributeHtml);
210  }
211  $attributeHtml = $this->process(
212  'categoryAttribute',
213  $attributeHtml,
214  ['category' => $category, 'attribute' => $attributeName]
215  );
216  return $attributeHtml;
217  }
218 
225  public function isDirectivesExists($attributeHtml)
226  {
227  $matches = false;
228  foreach ($this->directivePatterns as $pattern) {
229  if (preg_match($pattern, $attributeHtml)) {
230  $matches = true;
231  break;
232  }
233  }
234  return $matches;
235  }
236 }
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Eav\Model\Config $eavConfig, Data $catalogData, \Magento\Framework\Escaper $escaper, $directivePatterns=[])
Definition: Output.php:62
$pattern
Definition: website.php:22
process($method, $result, $params)
Definition: Output.php:130
addHandler($method, $handler)
Definition: Output.php:95
categoryAttribute($category, $attributeHtml, $attributeName)
Definition: Output.php:192
$method
Definition: info.phtml:13
productAttribute($product, $attributeHtml, $attributeName)
Definition: Output.php:150
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
isDirectivesExists($attributeHtml)
Definition: Output.php:225
catch(\Exception $e) $handler
Definition: index.php:30