[TASK] Add missing licence header to viewhelper classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / ViewHelpers / Link / EditRecordViewHelper.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\ViewHelpers\Link;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Backend\Routing\UriBuilder;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
21
22 /**
23 * Use this ViewHelper to provide edit links to records. The ViewHelper will
24 * pass the uid and table to FormEngine.
25 *
26 * The uid must be given as a positive integer.
27 * For new records, use the newRecordViewHelper
28 *
29 * Examples
30 * --------
31 *
32 * Link to the record-edit action passed to FormEngine::
33 *
34 * <be:link.editRecord uid="42" table="a_table" returnUrl="foo/bar" />
35 *
36 * Output::
37 *
38 * <a href="/typo3/index.php?route=/record/edit&edit[a_table][42]=edit&returnUrl=foo/bar">
39 * Edit record
40 * </a>
41 */
42 class EditRecordViewHelper extends AbstractTagBasedViewHelper
43 {
44 /**
45 * @var string
46 */
47 protected $tagName = 'a';
48
49 public function initializeArguments()
50 {
51 parent::initializeArguments();
52 $this->registerUniversalTagAttributes();
53 $this->registerArgument('uid', 'int', 'uid of record to be edited', true);
54 $this->registerArgument('table', 'string', 'target database table', true);
55 $this->registerArgument('returnUrl', 'string', '', false, '');
56 }
57
58 /**
59 * @return string
60 * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
61 */
62 public function render(): string
63 {
64 if ($this->arguments['uid'] < 1) {
65 throw new \InvalidArgumentException('Uid must be a positive integer, ' . $this->arguments['uid'] . ' given.', 1526127158);
66 }
67 if (empty($this->arguments['returnUrl'])) {
68 $this->arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
69 }
70
71 $params = [
72 'edit' => [$this->arguments['table'] => [$this->arguments['uid'] => 'edit']],
73 'returnUrl' => $this->arguments['returnUrl']
74 ];
75 $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
76 $uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
77 $this->tag->addAttribute('href', $uri);
78 $this->tag->setContent($this->renderChildren());
79 $this->tag->forceClosingTag(true);
80 return $this->tag->render();
81 }
82 }