Serializer.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\Widget\Grid;
  7. /**
  8. * @api
  9. * @deprecated 100.2.0 in favour of UI component implementation
  10. * @method string|array getInputNames()
  11. * @since 100.0.2
  12. */
  13. class Serializer extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * @var \Magento\Framework\Json\EncoderInterface
  17. */
  18. protected $_jsonEncoder;
  19. /**
  20. * @param \Magento\Framework\View\Element\Template\Context $context
  21. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  22. * @param array $data
  23. */
  24. public function __construct(
  25. \Magento\Framework\View\Element\Template\Context $context,
  26. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  27. array $data = []
  28. ) {
  29. $this->_jsonEncoder = $jsonEncoder;
  30. parent::__construct($context, $data);
  31. }
  32. /**
  33. * Preparing global layout
  34. *
  35. * @return $this
  36. */
  37. protected function _prepareLayout()
  38. {
  39. $grid = $this->getGridBlock();
  40. if (is_string($grid)) {
  41. $grid = $this->getLayout()->getBlock($grid);
  42. }
  43. if ($grid instanceof \Magento\Backend\Block\Widget\Grid) {
  44. $this->setGridBlock($grid)->setSerializeData($grid->{$this->getCallback()}());
  45. }
  46. return parent::_prepareLayout();
  47. }
  48. /**
  49. * Set serializer template
  50. *
  51. * @return void
  52. */
  53. public function _construct()
  54. {
  55. parent::_construct();
  56. $this->setTemplate('Magento_Backend::widget/grid/serializer.phtml');
  57. }
  58. /**
  59. * Get grid column input names to serialize
  60. *
  61. * @param bool $asJSON
  62. * @return string|array
  63. */
  64. public function getColumnInputNames($asJSON = false)
  65. {
  66. if ($asJSON) {
  67. return $this->_jsonEncoder->encode((array)$this->getInputNames());
  68. }
  69. return (array)$this->getInputNames();
  70. }
  71. /**
  72. * Get object data as JSON
  73. *
  74. * @return string
  75. */
  76. public function getDataAsJSON()
  77. {
  78. $result = [];
  79. $inputNames = $this->getInputNames();
  80. if ($serializeData = $this->getSerializeData()) {
  81. $result = $serializeData;
  82. } elseif (!empty($inputNames)) {
  83. return '{}';
  84. }
  85. return $this->_jsonEncoder->encode($result);
  86. }
  87. }