Skip to content

Commit 67807c1

Browse files
author
Oleksii Korshenko
committed
MAGETWO-69452: Remove zend json from form elements #9754
- Merge Pull Request #9754 from dmanners/magento2:remove-zend-json-from-form-elements - Merged commits: 1. 62c566d 2. b379aea 3. abb39b4
2 parents 1e19c93 + abb39b4 commit 67807c1

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

lib/internal/Magento/Framework/Data/Form/Element/Editablemultiselect.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,36 @@
1313
*/
1414
namespace Magento\Framework\Data\Form\Element;
1515

16+
use Magento\Framework\Escaper;
17+
1618
class Editablemultiselect extends \Magento\Framework\Data\Form\Element\Multiselect
1719
{
20+
/**
21+
* @var \Magento\Framework\Serialize\Serializer\Json
22+
*/
23+
private $serializer;
24+
25+
/**
26+
* Editablemultiselect constructor.
27+
* @param Factory $factoryElement
28+
* @param CollectionFactory $factoryCollection
29+
* @param Escaper $escaper
30+
* @param array $data
31+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
32+
* @throws \RuntimeException
33+
*/
34+
public function __construct(
35+
Factory $factoryElement,
36+
CollectionFactory $factoryCollection,
37+
Escaper $escaper,
38+
array $data = [],
39+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
40+
) {
41+
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
42+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
43+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
44+
}
45+
1846
/**
1947
* Name of the default JavaScript class that is used to make multiselect editable
2048
*
@@ -26,6 +54,7 @@ class Editablemultiselect extends \Magento\Framework\Data\Form\Element\Multisele
2654
* Retrieve HTML markup of the element
2755
*
2856
* @return string
57+
* @throws \InvalidArgumentException
2958
*/
3059
public function getElementHtml()
3160
{
@@ -41,7 +70,7 @@ public function getElementHtml()
4170
$elementJsClass = $this->getData('element_js_class');
4271
}
4372

44-
$selectConfigJson = \Zend_Json::encode($selectConfig);
73+
$selectConfigJson = $this->serializer->serialize($selectConfig);
4574
$jsObjectName = $this->getJsObjectName();
4675

4776
// TODO: TaxRateEditableMultiselect should be moved to a static .js module.

lib/internal/Magento/Framework/Data/Form/Element/Editor.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,25 @@
1616
class Editor extends Textarea
1717
{
1818
/**
19+
* @var \Magento\Framework\Serialize\Serializer\Json
20+
*/
21+
private $serializer;
22+
23+
/**
24+
* Editor constructor.
1925
* @param Factory $factoryElement
2026
* @param CollectionFactory $factoryCollection
2127
* @param Escaper $escaper
2228
* @param array $data
29+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
30+
* @throws \RuntimeException
2331
*/
2432
public function __construct(
2533
Factory $factoryElement,
2634
CollectionFactory $factoryCollection,
2735
Escaper $escaper,
28-
$data = []
36+
$data = [],
37+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
2938
) {
3039
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
3140

@@ -36,6 +45,8 @@ public function __construct(
3645
$this->setType('textarea');
3746
$this->setExtType('textarea');
3847
}
48+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
49+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
3950
}
4051

4152
/**
@@ -52,6 +63,21 @@ protected function getButtonTranslations()
5263
return $buttonTranslations;
5364
}
5465

66+
/**
67+
* @return bool|string
68+
* @throws \InvalidArgumentException
69+
*/
70+
private function getJsonConfig()
71+
{
72+
if (is_object($this->getConfig()) && method_exists($this->getConfig(), 'toJson')) {
73+
return $this->getConfig()->toJson();
74+
} else {
75+
return $this->serializer->serialize(
76+
$this->getConfig()
77+
);
78+
}
79+
}
80+
5581
/**
5682
* @return string
5783
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -132,7 +158,7 @@ public function getElementHtml()
132158
], function(jQuery){' .
133159
"\n" .
134160
' (function($) {$.mage.translate.add(' .
135-
\Zend_Json::encode(
161+
$this->serializer->serialize(
136162
$this->getButtonTranslations()
137163
) .
138164
')})(jQuery);' .
@@ -141,9 +167,7 @@ public function getElementHtml()
141167
' = new tinyMceWysiwygSetup("' .
142168
$this->getHtmlId() .
143169
'", ' .
144-
\Zend_Json::encode(
145-
$this->getConfig()
146-
) .
170+
$this->getJsonConfig() .
147171
');' .
148172
$forceLoad .
149173
'
@@ -180,7 +204,7 @@ public function getElementHtml()
180204
//<![CDATA[
181205
require(["jquery", "mage/translate", "mage/adminhtml/wysiwyg/widget"], function(jQuery){
182206
(function($) {
183-
$.mage.translate.add(' . \Zend_Json::encode($this->getButtonTranslations()) . ')
207+
$.mage.translate.add(' . $this->serializer->serialize($this->getButtonTranslations()) . ')
184208
})(jQuery);
185209
});
186210
//]]>

0 commit comments

Comments
 (0)