`.
*
* Examples
* ========
*
* Link to create a new record of a_table after record 17 on the same pid::
*
*
*
* Output::
*
*
* New record
*
*
* Link to create a new record of a_table on root page::
*
*
*
* Output::
*
*
* New record
*
*
* Link to create a new record of a_table on page 17::
*
*
*
* Output::
*
*
* New record
*
*
* Link to create a new record then return back to the BE module "web_MyextensionList"::
*
*
*
* Output::
*
*
* New record
*
*
* Link to create a new record of a_table on page 17 with a default value::
*
*
*
* Output::
*
*
* New record
*
*
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-backend-link-newrecord
*/
final class NewRecordViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var string
*/
protected $tagName = 'a';
public function __construct(
private readonly UriBuilder $uriBuilder
) {
parent::__construct();
}
public function initializeArguments(): void
{
parent::initializeArguments();
$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 new record 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', 1526129969);
}
if (isset($this->arguments['uid']) && $this->arguments['uid'] >= 0) {
throw new InvalidArgumentValueException('Uid must be negative integer, ' . $this->arguments['uid'] . ' given', 1526134901);
}
$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']],
// @todo add module argument to this view helper
'module' => ($this->arguments['module'] ?? '') ?: ($request?->getAttribute('module')?->getIdentifier() ?? ''),
'returnUrl' => $this->arguments['returnUrl'],
];
if ($this->arguments['defaultValues']) {
$params['defVals'] = $this->arguments['defaultValues'];
}
$uri = (string)$this->uriBuilder->buildUriFromRoute('record_edit', $params);
$this->tag->addAttribute('href', $uri);
$this->tag->setContent((string)$this->renderChildren());
$this->tag->forceClosingTag(true);
return $this->tag->render();
}
}