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