[BUGFIX] Field Wizards use FormEngine JS functions for has-change checks
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FieldControl / AddRecord.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Form\FieldControl;
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 use TYPO3\CMS\Backend\Form\AbstractNode;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Renders the icon with link parameters to add a new record,
23 * typically used for single elements of type=group or type=select.
24 */
25 class AddRecord extends AbstractNode
26 {
27 /**
28 * Add button control
29 *
30 * @return array As defined by FieldControl class
31 */
32 public function render(): array
33 {
34 $options = $this->data['renderData']['fieldControlOptions'];
35 $parameterArray = $this->data['parameterArray'];
36 $itemName = $parameterArray['itemFormElName'];
37
38 // Handle options and fallback
39 $title = $options['title'] ?? 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.createNew';
40 $setValue = $options['setValue'] ?? 'append';
41
42 $table = '';
43 if (isset($options['table'])) {
44 // Table given in options - use it
45 $table = $options['table'];
46 } elseif ($parameterArray['fieldConf']['config']['type'] === 'group'
47 && isset($parameterArray['fieldConf']['config']['internal_type'])
48 && $parameterArray['fieldConf']['config']['internal_type'] === 'db'
49 && !empty($parameterArray['fieldConf']['config']['allowed'])
50 ) {
51 // Use first table from allowed list if specific table is not set in options
52 $allowedTables = GeneralUtility::trimExplode(',', $parameterArray['fieldConf']['config']['allowed'], true);
53 $table = array_pop($allowedTables);
54 } elseif ($parameterArray['fieldConf']['config']['type'] === 'select'
55 && !empty($parameterArray['fieldConf']['config']['foreign_table'])
56 ) {
57 // Use foreign_table if given for type=select
58 $table = $parameterArray['fieldConf']['config']['foreign_table'];
59 }
60 if (empty($table)) {
61 // Still no table - this element can not handle the add control.
62 return [];
63 }
64
65 // Target pid of new records is current pid by default
66 $pid = $this->data['effectivePid'];
67 if (isset($options['pid'])) {
68 // pid configured in options - use it
69 $pid = $options['pid'];
70 } elseif (
71 isset($GLOBALS['TCA'][$table]['ctrl']['rootLevel'])
72 && (int)$GLOBALS['TCA'][$table]['ctrl']['rootLevel'] === 1
73 ) {
74 // Target table can only exist on root level - set 0 as pid
75 $pid = 0;
76 }
77
78 $prefixOfFormElName = 'data[' . $this->data['tableName'] . '][' . $this->data['databaseRow']['uid'] . '][' . $this->data['fieldName'] . ']';
79 $flexFormPath = '';
80 if (GeneralUtility::isFirstPartOfStr($itemName, $prefixOfFormElName)) {
81 $flexFormPath = str_replace('][', '/', substr($itemName, strlen($prefixOfFormElName) + 1, -1));
82 }
83
84 $urlParameters = [
85 'P' => [
86 'params' => [
87 'table' => $table,
88 'pid' => $pid,
89 'setValue' => $setValue,
90 ],
91 'table' => $this->data['tableName'],
92 'field' => $this->data['fieldName'],
93 'uid' => $this->data['databaseRow']['uid'],
94 'flexFormPath' => $flexFormPath,
95 'returnUrl' => $this->data['returnUrl'],
96 ],
97 ];
98
99 $onClick = [];
100 $onClick[] = 'this.blur();';
101 $onClick[] = 'return TYPO3.FormEngine.preventFollowLinkIfNotSaved(this.getAttribute(\'href\'));';
102 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
103 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
104 return [
105 'iconIdentifier' => 'actions-add',
106 'title' => $title,
107 'linkAttributes' => [
108 'onClick' => implode('', $onClick),
109 'href' => (string)$uriBuilder->buildUriFromRoute('wizard_add', $urlParameters),
110 ],
111 ];
112 }
113 }