[BUGFIX] toTop Link reloads page with baseUrl in fluid_styled_content
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid_styled_content / Classes / ViewHelpers / Link / SectionViewHelper.php
1 <?php
2 namespace TYPO3\CMS\FluidStyledContent\ViewHelpers\Link;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
18 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
19
20 /**
21 * A view helper for creating a link to a section.
22 * If baseUrl is used, the path part of the current URL is prefixed.
23 *
24 * = Example =
25 *
26 * <code title="section link">
27 * <ce:link.section name="section">Jump to section</ce:link.section>
28 * </code>
29 *
30 * <output>
31 * <a href="#section">Jump to section</a> or
32 * <a href="<path part of current URL>#section">Jump to section</a>
33 * </output>
34 */
35 class SectionViewHelper extends AbstractTagBasedViewHelper
36 {
37 /**
38 * @var string
39 */
40 protected $tagName = 'a';
41
42 /**
43 * Arguments initialization
44 *
45 * @return void
46 */
47 public function initializeArguments()
48 {
49 $this->registerUniversalTagAttributes();
50 }
51
52 /**
53 * Render the view helper
54 *
55 * @param string $name The section name to be used
56 * @return string
57 */
58 public function render($name)
59 {
60 $fragment = '#' . $name;
61
62 // Prefix with current URL path if baseUrl is used
63 if (!empty($this->getTypoScriptFrontendController()->baseUrl)) {
64 $fragment = $this->getTypoScriptFrontendController()->cObj->getUrlToCurrentLocation() . $fragment;
65 }
66
67 $this->tag->addAttribute('href', $fragment);
68 $this->tag->setContent($this->renderChildren());
69
70 return $this->tag->render();
71 }
72
73 /**
74 * @return TypoScriptFrontendController
75 */
76 protected function getTypoScriptFrontendController()
77 {
78 return $GLOBALS['TSFE'];
79 }
80 }