‪TYPO3CMS  10.4
CropViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23 
89 class ‪CropViewHelper extends AbstractViewHelper
90 {
91  use CompileWithRenderStatic;
92 
98  protected ‪$escapeOutput = false;
99 
105  public function ‪initializeArguments()
106  {
107  $this->registerArgument('maxCharacters', 'int', 'Place where to truncate the string', true);
108  $this->registerArgument('append', 'string', 'What to append, if truncation happened', false, '&hellip;');
109  $this->registerArgument('respectWordBoundaries', 'bool', 'If TRUE and division is in the middle of a word, the remains of that word is removed.', false, true);
110  $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);
111  }
112 
121  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
122  {
123  $maxCharacters = $arguments['maxCharacters'];
124  $append = $arguments['append'];
125  $respectWordBoundaries = $arguments['respectWordBoundaries'];
126  $respectHtml = $arguments['respectHtml'];
127 
128  $stringToTruncate = $renderChildrenClosure();
129 
130  // Even if we are in extbase/fluid context here, we're switching to a casual class of the framework here
131  // that has no dependency injection and other stuff. Therefor it is ok to use makeInstance instead of
132  // the ObjectManager here directly for additional performance
133  // Additionally, it would be possible to retrieve the "current" content object via ConfigurationManager->getContentObject(),
134  // but both crop() and cropHTML() are "nearly" static and do not depend on current content object settings, so
135  // it is safe to use a fresh instance here directly.
137  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
138  if ($respectHtml) {
139  $content = $contentObject->cropHTML($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
140  } else {
141  $content = $contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
142  }
143 
144  return $content;
145  }
146 }
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper\initializeArguments
‪initializeArguments()
Definition: CropViewHelper.php:103
‪TYPO3\CMS\Fluid\ViewHelpers\Format
Definition: AbstractEncodingViewHelper.php:16
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: CropViewHelper.php:119
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper
Definition: CropViewHelper.php:90
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper\$escapeOutput
‪bool $escapeOutput
Definition: CropViewHelper.php:96
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46