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