Skip to content

Commit

Permalink
MAGETWO-69452: Remove zend json from form elements #9754
Browse files Browse the repository at this point in the history
 - Merge Pull Request #9754 from dmanners/magento2:remove-zend-json-from-form-elements
 - Merged commits:
   1. 62c566d
   2. b379aea
   3. abb39b4
  • Loading branch information
Oleksii Korshenko committed Jun 28, 2017
2 parents 1e19c93 + abb39b4 commit 67807c1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,36 @@
*/
namespace Magento\Framework\Data\Form\Element;

use Magento\Framework\Escaper;

class Editablemultiselect extends \Magento\Framework\Data\Form\Element\Multiselect
{
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* Editablemultiselect constructor.
* @param Factory $factoryElement
* @param CollectionFactory $factoryCollection
* @param Escaper $escaper
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
* Name of the default JavaScript class that is used to make multiselect editable
*
Expand All @@ -26,6 +54,7 @@ class Editablemultiselect extends \Magento\Framework\Data\Form\Element\Multisele
* Retrieve HTML markup of the element
*
* @return string
* @throws \InvalidArgumentException
*/
public function getElementHtml()
{
Expand All @@ -41,7 +70,7 @@ public function getElementHtml()
$elementJsClass = $this->getData('element_js_class');
}

$selectConfigJson = \Zend_Json::encode($selectConfig);
$selectConfigJson = $this->serializer->serialize($selectConfig);
$jsObjectName = $this->getJsObjectName();

// TODO: TaxRateEditableMultiselect should be moved to a static .js module.
Expand Down
36 changes: 30 additions & 6 deletions lib/internal/Magento/Framework/Data/Form/Element/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@
class Editor extends Textarea
{
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* Editor constructor.
* @param Factory $factoryElement
* @param CollectionFactory $factoryCollection
* @param Escaper $escaper
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
$data = []
$data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);

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

/**
Expand All @@ -52,6 +63,21 @@ protected function getButtonTranslations()
return $buttonTranslations;
}

/**
* @return bool|string
* @throws \InvalidArgumentException
*/
private function getJsonConfig()
{
if (is_object($this->getConfig()) && method_exists($this->getConfig(), 'toJson')) {
return $this->getConfig()->toJson();
} else {
return $this->serializer->serialize(
$this->getConfig()
);
}
}

/**
* @return string
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
Expand Down Expand Up @@ -132,7 +158,7 @@ public function getElementHtml()
], function(jQuery){' .
"\n" .
' (function($) {$.mage.translate.add(' .
\Zend_Json::encode(
$this->serializer->serialize(
$this->getButtonTranslations()
) .
')})(jQuery);' .
Expand All @@ -141,9 +167,7 @@ public function getElementHtml()
' = new tinyMceWysiwygSetup("' .
$this->getHtmlId() .
'", ' .
\Zend_Json::encode(
$this->getConfig()
) .
$this->getJsonConfig() .
');' .
$forceLoad .
'
Expand Down Expand Up @@ -180,7 +204,7 @@ public function getElementHtml()
//<![CDATA[
require(["jquery", "mage/translate", "mage/adminhtml/wysiwyg/widget"], function(jQuery){
(function($) {
$.mage.translate.add(' . \Zend_Json::encode($this->getButtonTranslations()) . ')
$.mage.translate.add(' . $this->serializer->serialize($this->getButtonTranslations()) . ')
})(jQuery);
});
//]]>
Expand Down

0 comments on commit 67807c1

Please sign in to comment.