TYPO3 CMS  TYPO3_8-7
CropViewHelper.php
Go to the documentation of this file.
1 <?php
2 
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
23 
66 {
68 
74  protected $escapeOutput = false;
75 
82  public function initializeArguments()
83  {
84  $this->registerArgument('maxCharacters', 'int', 'Place where to truncate the string', true);
85  $this->registerArgument('append', 'string', 'What to append, if truncation happened', false, '...');
86  $this->registerArgument('respectWordBoundaries', 'bool', 'If TRUE and division is in the middle of a word, the remains of that word is removed.', false, true);
87  $this->registerArgument('respectHtml', 'bool', 'If TRUE the cropped string will respect HTML tags and entities. Technically that means, that cropHTML() is called rather than crop()', false, true);
88  }
89 
98  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
99  {
100  $maxCharacters = $arguments['maxCharacters'];
101  $append = $arguments['append'];
102  $respectWordBoundaries = $arguments['respectWordBoundaries'];
103  $respectHtml = $arguments['respectHtml'];
104 
105  $stringToTruncate = $renderChildrenClosure();
106 
107  // Even if we are in extbase/fluid context here, we're switching to a casual class of the framework here
108  // that has no dependency injection and other stuff. Therefor it is ok to use makeInstance instead of
109  // the ObjectManager here directly for additional performance
110  // Additionally, it would be possible to retrieve the "current" content object via ConfigurationManager->getContentObject(),
111  // but both crop() and cropHTML() are "nearly" static and do not depend on current content object settings, so
112  // it is safe to use a fresh instance here directly.
114  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
115  if ($respectHtml) {
116  $content = $contentObject->cropHTML($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
117  } else {
118  $content = $contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
119  }
120 
121  return $content;
122  }
123 }
static makeInstance($className,... $constructorArguments)