Skip to content

Commit e5508b4

Browse files
author
Oleksii Korshenko
authored
MAGETWO-69452: Remove zend json from form elements #9754
2 parents 5c516ad + d86bcde commit e5508b4

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
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
//]]>

lib/internal/Magento/Framework/Data/Test/Unit/Form/Element/EditorTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class EditorTest extends \PHPUnit_Framework_TestCase
4848
*/
4949
protected $objectManager;
5050

51+
/**
52+
* @var \PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
private $serializer;
55+
5156
protected function setUp()
5257
{
5358
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -62,13 +67,16 @@ protected function setUp()
6267
$this->escaperMock = $this->getMock(\Magento\Framework\Escaper::class, [], [], '', false);
6368
$this->configMock = $this->getMock(\Magento\Framework\DataObject::class, ['getData'], [], '', false);
6469

70+
$this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false);
71+
6572
$this->model = $this->objectManager->getObject(
6673
\Magento\Framework\Data\Form\Element\Editor::class,
6774
[
6875
'factoryElement' => $this->factoryMock,
6976
'factoryCollection' => $this->collectionFactoryMock,
7077
'escaper' => $this->escaperMock,
71-
'data' => ['config' => $this->configMock]
78+
'data' => ['config' => $this->configMock],
79+
'serializer' => $this->serializer
7280
]
7381
);
7482

@@ -203,7 +211,14 @@ public function testGetConfig()
203211
public function testGetTranslatedString()
204212
{
205213
$this->configMock->expects($this->any())->method('getData')->withConsecutive(['enabled'])->willReturn(true);
214+
$this->serializer->expects($this->any())
215+
->method('serialize')
216+
->willReturnCallback(function ($params) {
217+
return json_encode($params);
218+
}
219+
);
206220
$html = $this->model->getElementHtml();
221+
207222
$this->assertRegExp('/.*"Insert Image...":"Insert Image...".*/i', $html);
208223
}
209224
}

0 commit comments

Comments
 (0)