Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Configuration.php
Go to the documentation of this file.
1 <?php
7 
13 
20 {
26  protected $filter;
27 
34 
40  protected $string;
41 
45  private $serializer;
46 
50  private $escaper;
51 
59  public function __construct(
60  \Magento\Framework\App\Helper\Context $context,
61  \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
62  \Magento\Framework\Filter\FilterManager $filter,
63  \Magento\Framework\Stdlib\StringUtils $string,
64  Json $serializer = null,
65  Escaper $escaper = null
66  ) {
67  $this->_productOptionFactory = $productOptionFactory;
68  $this->filter = $filter;
69  $this->string = $string;
70  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
71  $this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
72  parent::__construct($context);
73  }
74 
81  public function getCustomOptions(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item)
82  {
83  $product = $item->getProduct();
84  $options = [];
85  $optionIds = $item->getOptionByCode('option_ids');
86  if ($optionIds) {
87  $options = [];
88  foreach (explode(',', $optionIds->getValue()) as $optionId) {
89  $option = $product->getOptionById($optionId);
90  if ($option) {
91  $itemOption = $item->getOptionByCode('option_' . $option->getId());
93  $group = $option->groupFactory($option->getType())
94  ->setOption($option)
95  ->setConfigurationItem($item)
96  ->setConfigurationItemOption($itemOption);
97 
98  if ('file' == $option->getType()) {
99  $downloadParams = $item->getFileDownloadParams();
100  if ($downloadParams) {
101  $url = $downloadParams->getUrl();
102  if ($url) {
103  $group->setCustomOptionDownloadUrl($url);
104  }
105  $urlParams = $downloadParams->getUrlParams();
106  if ($urlParams) {
107  $group->setCustomOptionUrlParams($urlParams);
108  }
109  }
110  }
111 
112  $options[] = [
113  'label' => $option->getTitle(),
114  'value' => $group->getFormattedOptionValue($itemOption->getValue()),
115  'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
116  'option_id' => $option->getId(),
117  'option_type' => $option->getType(),
118  'custom_view' => $group->isCustomizedView(),
119  ];
120  }
121  }
122  }
123 
124  $addOptions = $item->getOptionByCode('additional_options');
125  if ($addOptions) {
126  $options = array_merge($options, $this->serializer->unserialize($addOptions->getValue()));
127  }
128 
129  return $options;
130  }
131 
138  public function getOptions(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item)
139  {
140  return $this->getCustomOptions($item);
141  }
142 
170  {
171  // Init params
172  if (!$params) {
173  $params = [];
174  }
175  $maxLength = isset($params['max_length']) ? $params['max_length'] : null;
176  $cutReplacer = isset($params['cut_replacer']) ? $params['cut_replacer'] : '...';
177 
178  // Proceed with option
179  $optionInfo = [];
180 
181  // Define input data format
182  if (is_array($optionValue)) {
183  if (isset($optionValue['option_id'])) {
184  $optionInfo = $optionValue;
185  if (isset($optionInfo['value'])) {
186  $optionValue = $this->escaper->escapeHtml($optionInfo['value']);
187  }
188  } elseif (isset($optionValue['value'])) {
189  $optionValue = $optionValue['value'];
190  }
191  }
192 
193  // Render customized option view
194  if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
195  $_default = ['value' => $optionValue];
196  if (isset($optionInfo['option_type'])) {
197  try {
198  $group = $this->_productOptionFactory->create()->groupFactory($optionInfo['option_type']);
199  return ['value' => $group->getCustomizedView($optionInfo)];
200  } catch (\Exception $e) {
201  return $_default;
202  }
203  }
204  return $_default;
205  }
206 
207  // Truncate standard view
208  if (is_array($optionValue)) {
209  $truncatedValue = implode("\n", $optionValue);
210  $truncatedValue = nl2br($truncatedValue);
211  return ['value' => $truncatedValue];
212  } else {
213  if ($maxLength) {
214  $truncatedValue = $this->filter->truncate($optionValue, ['length' => $maxLength, 'etc' => '']);
215  } else {
216  $truncatedValue = $optionValue;
217  }
218  $truncatedValue = nl2br($truncatedValue);
219  }
220 
221  $result = ['value' => $truncatedValue];
222 
223  if ($maxLength && $this->string->strlen($optionValue) > $maxLength) {
224  $result['value'] = $result['value'] . $cutReplacer;
225  $optionValue = nl2br($optionValue);
226  $result['full_view'] = $optionValue;
227  }
228 
229  return $result;
230  }
231 }
getFormattedOptionValue($optionValue, $params=null)
getOptions(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item)
$_default
Definition: radio.phtml:13
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$group
Definition: sections.phtml:16
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory, \Magento\Framework\Filter\FilterManager $filter, \Magento\Framework\Stdlib\StringUtils $string, Json $serializer=null, Escaper $escaper=null)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18