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