|
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
|
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Backend\ViewHelpers\Link; |
|
19
|
|
|
|
|
20
|
|
|
use TYPO3\CMS\Backend\Routing\UriBuilder; |
|
21
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
22
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Use this ViewHelper to provide 'create new record' links. |
|
26
|
|
|
* The ViewHelper will pass the command to FormEngine. |
|
27
|
|
|
* |
|
28
|
|
|
* The table argument is mandatory, it decides what record is to be created. |
|
29
|
|
|
* |
|
30
|
|
|
* The pid argument will put the new record on this page, if ``0`` given it will |
|
31
|
|
|
* be placed to the root page. |
|
32
|
|
|
* |
|
33
|
|
|
* The uid argument accepts only negative values. If this is given, the new |
|
34
|
|
|
* record will be placed (by sorting field) behind the record with the uid. |
|
35
|
|
|
* It will end up on the same pid as this given record, so the pid must not |
|
36
|
|
|
* be given explicitly by pid argument. |
|
37
|
|
|
* |
|
38
|
|
|
* An exception will be thrown, if both uid and pid are given. |
|
39
|
|
|
* An exception will be thrown, if the uid argument is not a negative integer. |
|
40
|
|
|
* |
|
41
|
|
|
* To edit records, use the :ref:`<be:link.editRecordViewHelper> <typo3-backend-link-editrecord>`. |
|
42
|
|
|
* |
|
43
|
|
|
* Examples |
|
44
|
|
|
* ======== |
|
45
|
|
|
* |
|
46
|
|
|
* Link to create a new record of a_table after record 17 on the same pid:: |
|
47
|
|
|
* |
|
48
|
|
|
* <be:link.newRecord table="a_table" returnUrl="foo/bar" uid="-17"/> |
|
49
|
|
|
* |
|
50
|
|
|
* Output:: |
|
51
|
|
|
* |
|
52
|
|
|
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][-17]=new&returnUrl=foo/bar"> |
|
53
|
|
|
* Edit record |
|
54
|
|
|
* </a> |
|
55
|
|
|
* |
|
56
|
|
|
* Link to create a new record of a_table on root page:: |
|
57
|
|
|
* |
|
58
|
|
|
* <be:link.newRecord table="a_table" returnUrl="foo/bar""/> |
|
59
|
|
|
* |
|
60
|
|
|
* Output:: |
|
61
|
|
|
* |
|
62
|
|
|
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][]=new&returnUrl=foo/bar"> |
|
63
|
|
|
* Edit record |
|
64
|
|
|
* </a> |
|
65
|
|
|
* |
|
66
|
|
|
* Link to create a new record of a_table on page 17:: |
|
67
|
|
|
* |
|
68
|
|
|
* <be:link.newRecord table="a_table" returnUrl="foo/bar" pid="17"/> |
|
69
|
|
|
* |
|
70
|
|
|
* Output:: |
|
71
|
|
|
* |
|
72
|
|
|
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][17]=new&returnUrl=foo/bar"> |
|
73
|
|
|
* Edit record |
|
74
|
|
|
* </a> |
|
75
|
|
|
* |
|
76
|
|
|
* Link to create a new record then return back to the BE module "web_MyextensionList":: |
|
77
|
|
|
* |
|
78
|
|
|
* <be:link.newRecord table="a_table" returnUrl="{f:be.uri(route: 'web_MyextensionList')}" pid="17"> |
|
79
|
|
|
* |
|
80
|
|
|
* Output:: |
|
81
|
|
|
* |
|
82
|
|
|
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][17]=new&returnUrl=/typo3/index.php?route=/module/web/MyextensionList"> |
|
83
|
|
|
* Edit record |
|
84
|
|
|
* </a> |
|
85
|
|
|
* |
|
86
|
|
|
* Link to create a new record of a_table on page 17 with a default value:: |
|
87
|
|
|
* |
|
88
|
|
|
* <be:link.newRecord table="a_table" returnUrl="foo/bar" pid="17" defaultValues="{a_table: {a_field: 'value'}}"> |
|
89
|
|
|
* |
|
90
|
|
|
* Output:: |
|
91
|
|
|
* |
|
92
|
|
|
* <a href="/typo3/index.php?route=/record/edit&edit[a_table][17]=new&returnUrl=foo/bar&defVals[a_table][a_field]=value"> |
|
93
|
|
|
* Edit record |
|
94
|
|
|
* </a> |
|
95
|
|
|
*/ |
|
96
|
|
|
class NewRecordViewHelper extends AbstractTagBasedViewHelper |
|
97
|
|
|
{ |
|
98
|
|
|
/** |
|
99
|
|
|
* @var string |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $tagName = 'a'; |
|
102
|
|
|
|
|
103
|
|
|
public function initializeArguments() |
|
104
|
|
|
{ |
|
105
|
|
|
parent::initializeArguments(); |
|
106
|
|
|
$this->registerUniversalTagAttributes(); |
|
107
|
|
|
$this->registerArgument('uid', 'int', 'uid < 0 will insert the record after the given uid', false); |
|
108
|
|
|
$this->registerArgument('pid', 'int', 'the page id where the record will be created', false); |
|
109
|
|
|
$this->registerArgument('table', 'string', 'target database table', true); |
|
110
|
|
|
$this->registerArgument('returnUrl', 'string', 'return to this URL after closing the edit dialog', false, ''); |
|
111
|
|
|
$this->registerArgument('defaultValues', 'array', 'default values for fields of the new record', false, []); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string |
|
116
|
|
|
* @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function render(): string |
|
119
|
|
|
{ |
|
120
|
|
|
if ($this->arguments['uid'] && $this->arguments['pid']) { |
|
121
|
|
|
throw new \InvalidArgumentException('Can\'t handle both uid and pid for new records', 1526129969); |
|
122
|
|
|
} |
|
123
|
|
|
if (isset($this->arguments['uid']) && $this->arguments['uid'] >= 0) { |
|
124
|
|
|
throw new \InvalidArgumentException('Uid must be negative integer, ' . $this->arguments['uid'] . ' given', 1526134901); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (empty($this->arguments['returnUrl'])) { |
|
128
|
|
|
$this->arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$params = [ |
|
132
|
|
|
'edit' => [$this->arguments['table'] => [$this->arguments['uid'] ?? $this->arguments['pid'] ?? 0 => 'new']], |
|
133
|
|
|
'returnUrl' => $this->arguments['returnUrl'] |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
if (is_array($this->arguments['defaultValues']) && $this->arguments['defaultValues'] !== []) { |
|
137
|
|
|
$params['defVals'] = $this->arguments['defaultValues']; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class); |
|
141
|
|
|
$uri = (string)$uriBuilder->buildUriFromRoute('record_edit', $params); |
|
142
|
|
|
$this->tag->addAttribute('href', $uri); |
|
143
|
|
|
$this->tag->setContent($this->renderChildren()); |
|
144
|
|
|
$this->tag->forceClosingTag(true); |
|
145
|
|
|
return $this->tag->render(); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|