TYPO3 CMS  TYPO3_7-6
CropViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is part of the TYPO3 project - inspiring people to share! *
6  * *
7  * TYPO3 is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU General Public License version 2 as published by *
9  * the Free Software Foundation. *
10  * *
11  * This script is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14  * Public License for more details. *
15  * */
16 
23 
66 {
70  protected static $tsfeBackup;
71 
81  public function render($maxCharacters, $append = '...', $respectWordBoundaries = true, $respectHtml = true)
82  {
83  return static::renderStatic(
84  [
85  'maxCharacters' => $maxCharacters,
86  'append' => $append,
87  'respectWordBoundaries' => $respectWordBoundaries,
88  'respectHtml' => $respectHtml,
89  ],
91  $this->renderingContext
92  );
93  }
94 
102  {
103  $maxCharacters = $arguments['maxCharacters'];
104  $append = $arguments['append'];
105  $respectWordBoundaries = $arguments['respectWordBoundaries'];
106  $respectHtml = $arguments['respectHtml'];
107 
108  $stringToTruncate = $renderChildrenClosure();
109  if (TYPO3_MODE === 'BE') {
110  self::simulateFrontendEnvironment();
111  }
112 
113  // Even if we are in extbase/fluid context here, we're switching to a casual class of the framework here
114  // that has no dependency injection and other stuff. Therefor it is ok to use makeInstance instead of
115  // the ObjectManager here directly for additional performance
116  // Additionally, it would be possible to retrieve the "current" content object via ConfigurationManager->getContentObject(),
117  // but both crop() and cropHTML() are "nearly" static and do not depend on current content object settings, so
118  // it is safe to use a fresh instance here directly.
120  $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
121  if ($respectHtml) {
122  $content = $contentObject->cropHTML($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
123  } else {
124  $content = $contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
125  }
126  if (TYPO3_MODE === 'BE') {
127  self::resetFrontendEnvironment();
128  }
129  return $content;
130  }
131 
139  protected static function simulateFrontendEnvironment()
140  {
141  self::$tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : null;
142  $GLOBALS['TSFE'] = new \stdClass();
143  // preparing csConvObj
144  if (!is_object($GLOBALS['TSFE']->csConvObj)) {
145  if (is_object($GLOBALS['LANG'])) {
146  $GLOBALS['TSFE']->csConvObj = $GLOBALS['LANG']->csConvObj;
147  } else {
148  $GLOBALS['TSFE']->csConvObj = GeneralUtility::makeInstance(CharsetConverter::class);
149  }
150  }
151  // preparing renderCharset
152  if (!is_object($GLOBALS['TSFE']->renderCharset)) {
153  if (is_object($GLOBALS['LANG'])) {
154  $GLOBALS['TSFE']->renderCharset = $GLOBALS['LANG']->charSet;
155  } else {
156  $GLOBALS['TSFE']->renderCharset = 'utf-8';
157  }
158  }
159  }
160 
167  protected static function resetFrontendEnvironment()
168  {
169  $GLOBALS['TSFE'] = self::$tsfeBackup;
170  }
171 }
render($maxCharacters, $append='...', $respectWordBoundaries=true, $respectHtml=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static renderStatic(array $arguments, \Closure $renderChildrenClosure, \TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface $renderingContext)