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