Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Serializer.php
Go to the documentation of this file.
1 <?php
7 
9 
16 {
20  private $serializer;
21 
28  public function __construct(
29  Template\Context $context,
30  \Magento\Framework\Serialize\Serializer\Json $serializer = null,
31  array $data = []
32  ) {
33  parent::__construct($context, $data);
34  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
35  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
36  }
37 
41  public function _construct()
42  {
43  parent::_construct();
44  $this->setTemplate('Magento_Catalog::catalog/product/edit/serializer.phtml');
45  return $this;
46  }
47 
52  public function getProductsJSON()
53  {
54  $result = [];
55  if ($this->getProducts()) {
56  $isEntityId = $this->getIsEntityId();
57  foreach ($this->getProducts() as $product) {
58  $id = $isEntityId ? $product->getEntityId() : $product->getId();
59  $result[$id] = $product->toArray(['qty', 'position']);
60  }
61  }
62  return $result ? $this->serializer->serialize($result) : '{}';
63  }
64 }
__construct(Template\Context $context, \Magento\Framework\Serialize\Serializer\Json $serializer=null, array $data=[])
Definition: Serializer.php:28
$id
Definition: fieldset.phtml:14