[BUGFIX] Add $TSkey labels for internal debugging
[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 ViewHelper to crop the text between its opening and closing tags.
26 *
27 * Examples
28 * ========
29 *
30 * Defaults
31 * --------
32 *
33 * ::
34 *
35 * <f:format.crop maxCharacters="10">
36 * This is some very long text
37 * </f:format.crop>
38 *
39 * ``This is…``
40 *
41 * Custom suffix
42 * -------------
43 *
44 * ::
45 *
46 * <f:format.crop maxCharacters="17" append="&nbsp;[more]">
47 * This is some very long text
48 * </f:format.crop>
49 *
50 * ``This is some&nbsp;[more]``
51 *
52 * Don't respect word boundaries
53 * -----------------------------
54 *
55 * ::
56 *
57 * <f:format.crop maxCharacters="10" respectWordBoundaries="false">
58 * This is some very long text
59 * </f:format.crop>
60 *
61 * ``This is so…``
62 *
63 * Don't respect HTML tags
64 * -----------------------
65 *
66 * ::
67 *
68 * <f:format.crop maxCharacters="28" respectWordBoundaries="false" respectHtml="false">
69 * This is some text with <strong>HTML</strong> tags
70 * </f:format.crop>
71 *
72 * ``This is some text with <stro``
73 *
74 * Inline notation
75 * ---------------
76 *
77 * ::
78 *
79 * {someLongText -> f:format.crop(maxCharacters: 10)}
80 *
81 * ``someLongText cropped after 10 characters…``
82 * Depending on the value of ``{someLongText}``.
83 */
84 class CropViewHelper extends AbstractViewHelper
85 {
86 use CompileWithRenderStatic;
87
88 /**
89 * The output may contain HTML and can not be escaped.
90 *
91 * @var bool
92 */
93 protected $escapeOutput = false;
94
95 /**
96 * Initialize arguments.
97 *
98 * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
99 */
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
108 /**
109 * @param array $arguments
110 * @param \Closure $renderChildrenClosure
111 * @param RenderingContextInterface $renderingContext
112 *
113 * @return string
114 * @throws \InvalidArgumentException
115 */
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.
131 /** @var ContentObjectRenderer $contentObject */
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 }