Wrapline.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /**
  8. * Backend grid item renderer line to wrap
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Wrapline extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
  14. {
  15. /**
  16. * Default max length of a line at one row
  17. *
  18. * @var integer
  19. */
  20. protected $_defaultMaxLineLength = 60;
  21. /**
  22. * Magento string lib
  23. *
  24. * @var \Magento\Framework\Stdlib\StringUtils
  25. */
  26. protected $string;
  27. /**
  28. * @param \Magento\Backend\Block\Context $context
  29. * @param \Magento\Framework\Stdlib\StringUtils $string
  30. * @param array $data
  31. */
  32. public function __construct(
  33. \Magento\Backend\Block\Context $context,
  34. \Magento\Framework\Stdlib\StringUtils $string,
  35. array $data = []
  36. ) {
  37. $this->string = $string;
  38. parent::__construct($context, $data);
  39. }
  40. /**
  41. * Renders grid column
  42. *
  43. * @param \Magento\Framework\DataObject $row
  44. * @return string
  45. */
  46. public function render(\Magento\Framework\DataObject $row)
  47. {
  48. $line = parent::_getValue($row);
  49. $wrappedLine = '';
  50. $lineLength = $this->getColumn()->getData(
  51. 'lineLength'
  52. ) ? $this->getColumn()->getData(
  53. 'lineLength'
  54. ) : $this->_defaultMaxLineLength;
  55. for ($i = 0, $n = floor($this->string->strlen($line) / $lineLength); $i <= $n; $i++) {
  56. $wrappedLine .= $this->string->substr($line, $lineLength * $i, $lineLength) . "<br />";
  57. }
  58. return $wrappedLine;
  59. }
  60. }