‪TYPO3CMS  9.5
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 
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23 
84 class ‪CropViewHelper extends AbstractViewHelper
85 {
86  use CompileWithRenderStatic;
87 
93  protected ‪$escapeOutput = false;
94 
100  public function ‪initializeArguments()
101  {
102  $this->registerArgument('maxCharacters', 'int', 'Place where to truncate the string', true);
103  $this->registerArgument('append', 'string', 'What to append, if truncation happened', false, '&hellip;');
104  $this->registerArgument('respectWordBoundaries', 'bool', 'If TRUE and division is in the middle of a word, the remains of that word is removed.', false, true);
105  $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);
106  }
107 
116  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
117  {
118  $maxCharacters = $arguments['maxCharacters'];
119  $append = $arguments['append'];
120  $respectWordBoundaries = $arguments['respectWordBoundaries'];
121  $respectHtml = $arguments['respectHtml'];
122 
123  $stringToTruncate = $renderChildrenClosure();
124 
125  // Even if we are in extbase/fluid context here, we're switching to a casual class of the framework here
126  // that has no dependency injection and other stuff. Therefor it is ok to use makeInstance instead of
127  // the ObjectManager here directly for additional performance
128  // Additionally, it would be possible to retrieve the "current" content object via ConfigurationManager->getContentObject(),
129  // but both crop() and cropHTML() are "nearly" static and do not depend on current content object settings, so
130  // it is safe to use a fresh instance here directly.
132  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
133  if ($respectHtml) {
134  $content = $contentObject->cropHTML($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
135  } else {
136  $content = $contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
137  }
138 
139  return $content;
140  }
141 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper\initializeArguments
‪initializeArguments()
Definition: CropViewHelper.php:98
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CropViewHelper.php:114
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper
Definition: CropViewHelper.php:85
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:91
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: CropViewHelper.php:91
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45