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