[TASK] Streamline phpdoc annotations in EXT:fluid
[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 class LinkViewHelper extends AbstractTagBasedViewHelper
33 {
34 /**
35 * @var string
36 */
37 protected $tagName = 'a';
38
39 /**
40 * Initialize arguments
41 */
42 public function initializeArguments()
43 {
44 parent::initializeArguments();
45 $this->registerUniversalTagAttributes();
46 $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
47 $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
48 $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
49 $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
50 $this->registerArgument('useCacheHash', 'bool', 'True whether the cache hash should be appended to the URL', false, false);
51 $this->registerArgument('addQueryStringMethod', 'string', 'Method to be used for query string');
52 $this->registerArgument('action', 'string', 'Target action');
53 $this->registerArgument('arguments', 'array', 'Arguments', false, []);
54 $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
55 $this->registerArgument('format', 'string', 'The requested format, e.g. ".html', false, '');
56 $this->registerArgument('ajax', 'bool', 'TRUE if the URI should be to an AJAX widget, FALSE otherwise.', false, false);
57 }
58
59 /**
60 * Render the link.
61 *
62 * @return string The rendered link
63 */
64 public function render()
65 {
66 $ajax = $this->arguments['ajax'];
67
68 if ($ajax === true) {
69 $uri = $this->getAjaxUri();
70 } else {
71 $uri = $this->getWidgetUri();
72 }
73 $this->tag->addAttribute('href', $uri);
74 $this->tag->setContent($this->renderChildren());
75 return $this->tag->render();
76 }
77
78 /**
79 * Get the URI for an AJAX Request.
80 *
81 * @return string the AJAX URI
82 */
83 protected function getAjaxUri()
84 {
85 $action = $this->arguments['action'];
86 $arguments = $this->arguments['arguments'];
87 if ($action === null) {
88 $action = $this->renderingContext->getControllerContext()->getRequest()->getControllerActionName();
89 }
90 $arguments['id'] = $GLOBALS['TSFE']->id;
91 // @todo page type should be configurable
92 $arguments['type'] = 7076;
93 $arguments['fluid-widget-id'] = $this->renderingContext->getControllerContext()->getRequest()->getWidgetContext()->getAjaxWidgetIdentifier();
94 $arguments['action'] = $action;
95 return '?' . http_build_query($arguments, null, '&');
96 }
97
98 /**
99 * Get the URI for a non-AJAX Request.
100 *
101 * @return string the Widget URI
102 */
103 protected function getWidgetUri()
104 {
105 $uriBuilder = $this->renderingContext->getControllerContext()->getUriBuilder();
106 $argumentPrefix = $this->renderingContext->getControllerContext()->getRequest()->getArgumentPrefix();
107 $arguments = $this->hasArgument('arguments') ? $this->arguments['arguments'] : [];
108 if ($this->hasArgument('action')) {
109 $arguments['action'] = $this->arguments['action'];
110 }
111 if ($this->hasArgument('format') && $this->arguments['format'] !== '') {
112 $arguments['format'] = $this->arguments['format'];
113 }
114 return $uriBuilder->reset()
115 ->setArguments([$argumentPrefix => $arguments])
116 ->setSection($this->arguments['section'])
117 ->setUseCacheHash($this->arguments['useCacheHash'])
118 ->setAddQueryString(true)
119 ->setAddQueryStringMethod($this->arguments['addQueryStringMethod'])
120 ->setArgumentsToBeExcludedFromQueryString([$argumentPrefix, 'cHash'])
121 ->setFormat($this->arguments['format'])
122 ->build();
123 }
124 }