‪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\ViewHelper\AbstractTagBasedViewHelper;
23 
80 class ‪NewRecordViewHelper extends AbstractTagBasedViewHelper
81 {
85  protected ‪$tagName = 'a';
86 
87  public function ‪initializeArguments()
88  {
89  parent::initializeArguments();
90  $this->registerUniversalTagAttributes();
91  $this->registerArgument('uid', 'int', 'uid < 0 will insert the record after the given uid', false);
92  $this->registerArgument('pid', 'int', 'the page id where the record will be created', false);
93  $this->registerArgument('table', 'string', 'target database table', true);
94  $this->registerArgument('returnUrl', 'string', 'return to this URL after closing the edit dialog', false, '');
95  }
96 
101  public function ‪render(): string
102  {
103  if ($this->arguments['uid'] && $this->arguments['pid']) {
104  throw new \InvalidArgumentException('Can\'t handle both uid and pid for new records', 1526129969);
105  }
106  if (isset($this->arguments['uid']) && $this->arguments['uid'] >= 0) {
107  throw new \InvalidArgumentException('Uid must be negative integer, ' . $this->arguments['uid'] . ' given', 1526134901);
108  }
109 
110  if (empty($this->arguments['returnUrl'])) {
111  $this->arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
112  }
113 
114  $params = [
115  'edit' => [$this->arguments['table'] => [$this->arguments['uid'] ?? $this->arguments['pid'] ?? 0 => 'new']],
116  'returnUrl' => $this->arguments['returnUrl']
117  ];
118  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
119  $uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
120  $this->tag->addAttribute('href', $uri);
121  $this->tag->setContent($this->renderChildren());
122  $this->tag->forceClosingTag(true);
123  return $this->tag->render();
124  }
125 }
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46