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