SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/ColModifierTruncate.php

  1. <?php
  2. namespace Sprout\Helpers;
  3.  
  4. use Sprout\Helpers\Enc;
  5. use Sprout\Helpers\Text;
  6. use Sprout\Helpers\UnescapedColModifier;
  7.  
  8.  
  9. /**
  10.  * Truncate the word count of a column with given word limit trailed by ellipsis symbol, with the full text in a title attribute
  11.  */
  12. final class ColModifierTruncate extends UnescapedColModifier
  13. {
  14. private $word_limit = 4;
  15.  
  16. public function __construct($word_limit = 4)
  17. {
  18. $this->word_limit = max((int)$word_limit, 1);
  19. }
  20.  
  21. public function modify($val, $field_name)
  22. {
  23. return '<span title="' . Enc::html($val) . '">' . Enc::html(Text::plain($val, $this->word_limit)) . '</span>';
  24. }
  25. }
  26.