‪TYPO3CMS  9.5
NewRecordViewHelper.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
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 
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
24 
65 class ‪NewRecordViewHelper extends AbstractTagBasedViewHelper
66 {
67  use CompileWithRenderStatic;
68 
69  public function ‪initializeArguments()
70  {
71  $this->registerArgument('uid', 'int', 'uid < 0 will insert the record after the given uid', false);
72  $this->registerArgument('pid', 'int', 'the page id where the record will be created', false);
73  $this->registerArgument('table', 'string', 'target database table', true);
74  $this->registerArgument('returnUrl', 'string', '', false, '');
75  }
76 
85  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
86  {
87  if ($arguments['uid'] && $arguments['pid']) {
88  throw new \InvalidArgumentException('Can\'t handle both uid and pid for new records', 1526136338);
89  }
90  if (isset($arguments['uid']) && $arguments['uid'] >= 0) {
91  throw new \InvalidArgumentException('Uid must be negative integer, ' . $arguments['uid'] . ' given', 1526136362);
92  }
93 
94  if (empty($arguments['returnUrl'])) {
95  $arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
96  }
97 
98  $params = [
99  'edit' => [$arguments['table'] => [$arguments['uid'] ?? $arguments['pid'] ?? 0 => 'new']],
100  'returnUrl' => $arguments['returnUrl']
101  ];
102  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
103  return (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
104  }
105 }
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:35
‪TYPO3\CMS\Backend\ViewHelpers\Uri
Definition: EditRecordViewHelper.php:4
‪TYPO3\CMS\Backend\ViewHelpers\Uri\NewRecordViewHelper\initializeArguments
‪initializeArguments()
Definition: NewRecordViewHelper.php:68
‪TYPO3\CMS\Backend\ViewHelpers\Uri\NewRecordViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: NewRecordViewHelper.php:84
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\ViewHelpers\Uri\NewRecordViewHelper
Definition: NewRecordViewHelper.php:66