* * ``` * * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-backend-uri-newrecord * @see https://docs.typo3.org/permalink/t3viewhelper:typo3-backend-uri-editrecord */ final class NewRecordViewHelper extends AbstractTagBasedViewHelper { public function __construct( private readonly UriBuilder $uriBuilder ) { parent::__construct(); } public function initializeArguments(): void { $this->registerArgument('uid', 'int', 'uid < 0 will insert the record after the given uid'); $this->registerArgument('pid', 'int', 'the page id where the record will be created'); $this->registerArgument('table', 'string', 'target database table', true); $this->registerArgument('module', 'string', 'Set module identifier for context - marking as acitve when editing the record', false, ''); $this->registerArgument('returnUrl', 'string', 'return to this URL after closing the edit dialog', false, ''); $this->registerArgument('defaultValues', 'array', 'default values for fields of the new record', false, []); } /** * @throws InvalidArgumentException * @throws RouteNotFoundException */ public function render(): string { if ($this->arguments['uid'] && $this->arguments['pid']) { throw new InvalidArgumentException('Can\'t handle both uid and pid for new records', 1526136338); } if (isset($this->arguments['uid']) && $this->arguments['uid'] >= 0) { throw new InvalidArgumentValueException('Uid must be negative integer, ' . $this->arguments['uid'] . ' given', 1526136362); } $request = $this->renderingContext->hasAttribute(ServerRequestInterface::class) ? $this->renderingContext->getAttribute(ServerRequestInterface::class) : null; if (empty($this->arguments['returnUrl']) && $request !== null) { $this->arguments['returnUrl'] = $request->getAttribute('normalizedParams')->getRequestUri(); } $params = [ 'edit' => [$this->arguments['table'] => [$this->arguments['uid'] ?? $this->arguments['pid'] ?? 0 => 'new']], 'module' => ($this->arguments['module'] ?? '') ?: ($request?->getAttribute('module')?->getIdentifier() ?? ''), 'returnUrl' => $this->arguments['returnUrl'], ]; if ($this->arguments['defaultValues']) { $params['defVals'] = $this->arguments['defaultValues']; } return (string)$this->uriBuilder->buildUriFromRoute('record_edit', $params); } }