0d0dc0c8831dea02529e0699c37e0c5b7fd2b775
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Widget / LinkViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Widget;
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 TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
18
19 /**
20 * A view helper for creating Links to extbase actions within widets.
21 *
22 * = Examples =
23 *
24 * <code title="URI to the show-action of the current controller">
25 * <f:widget.link action="show">link</f:widget.link>
26 * </code>
27 * <output>
28 * <a href="index.php?id=123&tx_myextension_plugin[widgetIdentifier][action]=show&tx_myextension_plugin[widgetIdentifier][controller]=Standard&cHash=xyz">link</a>
29 * (depending on the current page, widget and your TS configuration)
30 * </output>
31 *
32 * @api
33 */
34 class LinkViewHelper extends AbstractTagBasedViewHelper
35 {
36 /**
37 * @var string
38 */
39 protected $tagName = 'a';
40
41 /**
42 * Initialize arguments
43 *
44 * @api
45 */
46 public function initializeArguments()
47 {
48 parent::initializeArguments();
49 $this->registerUniversalTagAttributes();
50 $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
51 $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
52 $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
53 $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
54 $this->registerArgument('useCacheHash', 'bool', 'True whether the cache hash should be appended to the URL', false, false);
55 $this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
56 $this->registerArgument('action', 'string', 'Target action');
57 $this->registerArgument('arguments', 'array', 'Arguments', false, []);
58 $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
59 $this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
60 $this->registerArgument('ajax', 'bool', 'TRUE if the URI should be to an AJAX widget, FALSE otherwise.', false, false);
61 }
62
63 /**
64 * Render the link.
65 *
66 * @return string The rendered link
67 * @api
68 */
69 public function render()
70 {
71 $ajax = $this->arguments['ajax'];
72
73 if ($ajax === true) {
74 $uri = $this->getAjaxUri();
75 } else {
76 $uri = $this->getWidgetUri();
77 }
78 $this->tag->addAttribute('href', $uri);
79 $this->tag->setContent($this->renderChildren());
80 return $this->tag->render();
81 }
82
83 /**
84 * Get the URI for an AJAX Request.
85 *
86 * @return string the AJAX URI
87 */
88 protected function getAjaxUri()
89 {
90 $action = $this->arguments['action'];
91 $arguments = $this->arguments['arguments'];
92 if ($action === null) {
93 $action = $this->renderingContext->getControllerContext()->getRequest()->getControllerActionName();
94 }
95 $arguments['id'] = $GLOBALS['TSFE']->id;
96 // @todo page type should be configurable
97 $arguments['type'] = 7076;
98 $arguments['fluid-widget-id'] = $this->renderingContext->getControllerContext()->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
99 $arguments['action'] = $action;
100 return '?' . http_build_query($arguments, null, '&');
101 }
102
103 /**
104 * Get the URI for a non-AJAX Request.
105 *
106 * @return string the Widget URI
107 */
108 protected function getWidgetUri()
109 {
110 $uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
111 $argumentPrefix = $this->renderingContext->getControllerContext()->getRequest()->getArgumentPrefix();
112 $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : [];
113 if ($this->hasArgument('action')) {
114 $arguments['action'] = $this->arguments['action'];
115 }
116 if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
117 $arguments['format'] = $this->arguments['format'];
118 }
119 return $uriBuilder->reset()
120 ->setArguments([$argumentPrefix => $arguments])
121 ->setSection($this->arguments['section'])
122 ->setUseCacheHash($this->arguments['useCacheHash'])
123 ->setAddQueryString(true)
124 ->setAddQueryStringMethod($this->arguments['addQueryStringMethod'])
125 ->setArgumentsToBeExcludedFromQueryString([$argumentPrefix, 'cHash'])
126 ->setFormat($this->arguments['format'])
127 ->build();
128 }
129 }