‪TYPO3CMS  10.4
AddRecord.php
Go to the documentation of this file.
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 
19 
25 
31 {
37  public function ‪render(): array
38  {
39  $options = $this->data['renderData']['fieldControlOptions'];
40  $parameterArray = $this->data['parameterArray'];
41  $itemName = $parameterArray['itemFormElName'];
42 
43  // Handle options and fallback
44  $title = $options['title'] ?? 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.createNew';
45  $setValue = $options['setValue'] ?? 'append';
46 
47  $table = '';
48  if (isset($options['table'])) {
49  // Table given in options - use it
50  $table = $options['table'];
51  } elseif ($parameterArray['fieldConf']['config']['type'] === 'group'
52  && isset($parameterArray['fieldConf']['config']['internal_type'])
53  && $parameterArray['fieldConf']['config']['internal_type'] === 'db'
54  && !empty($parameterArray['fieldConf']['config']['allowed'])
55  ) {
56  // Use first table from allowed list if specific table is not set in options
57  $allowedTables = ‪GeneralUtility::trimExplode(',', $parameterArray['fieldConf']['config']['allowed'], true);
58  $table = array_pop($allowedTables);
59  } elseif ($parameterArray['fieldConf']['config']['type'] === 'select'
60  && !empty($parameterArray['fieldConf']['config']['foreign_table'])
61  ) {
62  // Use foreign_table if given for type=select
63  $table = $parameterArray['fieldConf']['config']['foreign_table'];
64  }
65  if (empty($table)) {
66  // Still no table - this element can not handle the add control.
67  return [];
68  }
69 
70  // Target pid of new records is current pid by default
71  $pid = $this->data['effectivePid'];
72  if (isset($options['pid'])) {
73  // pid configured in options - use it
74  if ($options['pid'] === '###SITEROOT###' && ($this->data['site'] ?? null) instanceof ‪Site) {
75  // Substitute marker with pid from site object
76  $pid = $this->data['site']->getRootPageId();
77  } else {
78  $pid = $options['pid'];
79  }
80  } elseif (
81  isset(‪$GLOBALS['TCA'][$table]['ctrl']['rootLevel'])
82  && (int)‪$GLOBALS['TCA'][$table]['ctrl']['rootLevel'] === 1
83  ) {
84  // Target table can only exist on root level - set 0 as pid
85  $pid = 0;
86  }
87 
88  $prefixOfFormElName = 'data[' . $this->data['tableName'] . '][' . $this->data['databaseRow']['uid'] . '][' . $this->data['fieldName'] . ']';
89  $flexFormPath = '';
90  if (GeneralUtility::isFirstPartOfStr($itemName, $prefixOfFormElName)) {
91  $flexFormPath = str_replace('][', '/', substr($itemName, strlen($prefixOfFormElName) + 1, -1));
92  }
93 
94  $urlParameters = [
95  'P' => [
96  'params' => [
97  'table' => $table,
98  'pid' => $pid,
99  'setValue' => $setValue,
100  ],
101  'table' => $this->data['tableName'],
102  'field' => $this->data['fieldName'],
103  'uid' => $this->data['databaseRow']['uid'],
104  'flexFormPath' => $flexFormPath,
105  'returnUrl' => $this->data['returnUrl'],
106  ],
107  ];
108 
109  $id = ‪StringUtility::getUniqueId('t3js-formengine-fieldcontrol-');
110 
112  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
113  return [
114  'iconIdentifier' => 'actions-add',
115  'title' => $title,
116  'linkAttributes' => [
117  'id' => htmlspecialchars($id),
118  'href' => (string)$uriBuilder->buildUriFromRoute('wizard_add', $urlParameters),
119  ],
120  'requireJsModules' => [
121  ['TYPO3/CMS/Backend/FormEngine/FieldControl/AddRecord' => 'function(FieldControl) {new FieldControl(' . GeneralUtility::quoteJSvalue('#' . $id) . ');}'],
122  ],
123  ];
124  }
125 }
‪TYPO3\CMS\Backend\Form\FieldControl
Definition: AddRecord.php:18
‪TYPO3\CMS\Backend\Form\FieldControl\AddRecord\render
‪array render()
Definition: AddRecord.php:37
‪TYPO3\CMS\Backend\Form\FieldControl\AddRecord
Definition: AddRecord.php:31
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Form\AbstractNode
Definition: AbstractNode.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22