[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / CropViewHelper.php
1 <?php
2
3 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
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
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24 /**
25 * Use this view helper to crop the text between its opening and closing tags.
26 *
27 * = Examples =
28 *
29 * <code title="Defaults">
30 * <f:format.crop maxCharacters="10">This is some very long text</f:format.crop>
31 * </code>
32 * <output>
33 * This is…
34 * </output>
35 *
36 * <code title="Custom suffix">
37 * <f:format.crop maxCharacters="17" append="&nbsp;[more]">This is some very long text</f:format.crop>
38 * </code>
39 * <output>
40 * This is some&nbsp;[more]
41 * </output>
42 *
43 * <code title="Don't respect word boundaries">
44 * <f:format.crop maxCharacters="10" respectWordBoundaries="false">This is some very long text</f:format.crop>
45 * </code>
46 * <output>
47 * This is so…
48 * </output>
49 *
50 * <code title="Don't respect HTML tags">
51 * <f:format.crop maxCharacters="28" respectWordBoundaries="false" respectHtml="false">This is some text with <strong>HTML</strong> tags</f:format.crop>
52 * </code>
53 * <output>
54 * This is some text with <stro
55 * </output>
56 *
57 * <code title="Inline notation">
58 * {someLongText -> f:format.crop(maxCharacters: 10)}
59 * </code>
60 * <output>
61 * someLongText cropped after 10 characters…
62 * (depending on the value of {someLongText})
63 * </output>
64 */
65 class CropViewHelper extends AbstractViewHelper
66 {
67 use CompileWithRenderStatic;
68
69 /**
70 * The output may contain HTML and can not be escaped.
71 *
72 * @var bool
73 */
74 protected $escapeOutput = false;
75
76 /**
77 * Initialize arguments.
78 *
79 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
80 */
81 public function initializeArguments()
82 {
83 $this->registerArgument('maxCharacters', 'int', 'Place where to truncate the string', true);
84 $this->registerArgument('append', 'string', 'What to append, if truncation happened', false, '&hellip;');
85 $this->registerArgument('respectWordBoundaries', 'bool', 'If TRUE and division is in the middle of a word, the remains of that word is removed.', false, true);
86 $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);
87 }
88
89 /**
90 * @param array $arguments
91 * @param \Closure $renderChildrenClosure
92 * @param RenderingContextInterface $renderingContext
93 *
94 * @return string
95 * @throws \InvalidArgumentException
96 */
97 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
98 {
99 $maxCharacters = $arguments['maxCharacters'];
100 $append = $arguments['append'];
101 $respectWordBoundaries = $arguments['respectWordBoundaries'];
102 $respectHtml = $arguments['respectHtml'];
103
104 $stringToTruncate = $renderChildrenClosure();
105
106 // Even if we are in extbase/fluid context here, we're switching to a casual class of the framework here
107 // that has no dependency injection and other stuff. Therefor it is ok to use makeInstance instead of
108 // the ObjectManager here directly for additional performance
109 // Additionally, it would be possible to retrieve the "current" content object via ConfigurationManager->getContentObject(),
110 // but both crop() and cropHTML() are "nearly" static and do not depend on current content object settings, so
111 // it is safe to use a fresh instance here directly.
112 /** @var ContentObjectRenderer $contentObject */
113 $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
114 if ($respectHtml) {
115 $content = $contentObject->cropHTML($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
116 } else {
117 $content = $contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
118 }
119
120 return $content;
121 }
122 }