Text.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Column\Renderer;
  7. use Magento\Framework\DataObject;
  8. /**
  9. * Backend grid item renderer
  10. *
  11. * @api
  12. * @deprecated 100.2.0 in favour of UI component implementation
  13. * @since 100.0.2
  14. */
  15. class Text extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
  16. {
  17. /**
  18. * Format variables pattern
  19. *
  20. * @var string
  21. */
  22. protected $_variablePattern = '/\\$([a-z0-9_]+)/i';
  23. /**
  24. * Get value for the cel
  25. *
  26. * @param DataObject $row
  27. * @return string
  28. */
  29. public function _getValue(DataObject $row)
  30. {
  31. if (null === $this->getColumn()->getFormat()) {
  32. return $this->getSimpleValue($row);
  33. }
  34. return $this->getFormattedValue($row);
  35. }
  36. /**
  37. * Get simple value
  38. *
  39. * @param DataObject $row
  40. * @return string
  41. */
  42. private function getSimpleValue(DataObject $row)
  43. {
  44. $data = parent::_getValue($row);
  45. $value = null === $data ? $this->getColumn()->getDefault() : $data;
  46. if (true === $this->getColumn()->getTranslate()) {
  47. $value = __($value);
  48. }
  49. return $this->escapeHtml($value);
  50. }
  51. /**
  52. * Replace placeholders in the string with values
  53. *
  54. * @param DataObject $row
  55. * @return string
  56. */
  57. private function getFormattedValue(DataObject $row)
  58. {
  59. $value = $this->getColumn()->getFormat() ?: null;
  60. if (true === $this->getColumn()->getTranslate()) {
  61. $value = __($value);
  62. }
  63. if (preg_match_all($this->_variablePattern, $value, $matches)) {
  64. foreach ($matches[0] as $index => $match) {
  65. $replacement = $row->getData($matches[1][$index]);
  66. $value = str_replace($match, $replacement, $value);
  67. }
  68. }
  69. return $this->escapeHtml($value);
  70. }
  71. }