d1b5fad0a9c9a88b8e107e295ee7896d78d0e3e8
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FieldControl / ListModule.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 jump to the list module
23 * "single table" view, showing only one configurable table.
24 */
25 class ListModule extends AbstractNode
26 {
27 /**
28 * Add button control
29 *
30 * @return array As defined by FieldControl class
31 */
32 public function render()
33 {
34 $options = $this->data['renderData']['fieldControlOptions'];
35 $parameterArray = $this->data['parameterArray'];
36
37 // Handle options and fallback
38 $title = $options['title'] ?? 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.list';
39
40 $table = '';
41 if (isset($options['table'])) {
42 // Table given in options - use it
43 $table = $options['table'];
44 } elseif ($parameterArray['fieldConf']['config']['type'] === 'group'
45 && isset($parameterArray['fieldConf']['config']['internal_type'])
46 && $parameterArray['fieldConf']['config']['internal_type'] === 'db'
47 && !empty($parameterArray['fieldConf']['config']['allowed'])
48 ) {
49 // Use first table from allowed list if specific table is not set in options
50 $allowedTables = GeneralUtility::trimExplode(',', $parameterArray['fieldConf']['config']['allowed'], true);
51 $table = array_pop($allowedTables);
52 } elseif ($parameterArray['fieldConf']['config']['type'] === 'select'
53 && !empty($parameterArray['fieldConf']['config']['foreign_table'])
54 ) {
55 // Use foreign_table if given for type=select
56 $table = $parameterArray['fieldConf']['config']['foreign_table'];
57 }
58 if (empty($table)) {
59 // Still no table - this element can not handle the list control.
60 return [];
61 }
62
63 // Target pid of new records is current pid by default
64 $pid = $this->data['effectivePid'];
65 if (isset($options['pid'])) {
66 // pid configured in options - use it
67 $pid = $options['pid'];
68 } elseif (isset($GLOBALS['TCA'][$table]['ctrl']['rootLevel']) && (int)$GLOBALS['TCA'][$table]['ctrl'] === 1) {
69 // Target table can only exist on root level - set 0 as pid
70 $pid = 0;
71 }
72
73 $urlParameters = [
74 'P' => [
75 'params' => [
76 'table' => $table,
77 'pid' => $pid,
78 ],
79 'table' => $this->data['tableName'],
80 'uid' => $this->data['databaseRow']['uid'],
81 'returnUrl' => $this->data['returnUrl'],
82 ],
83 ];
84
85 $onClick = [];
86 $onClick[] = 'this.blur();';
87 $onClick[] = 'return TYPO3.FormEngine.preventFollowLinkIfNotSaved(this.getAttribute(\'href\'));';
88 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
89 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
90
91 return [
92 'iconIdentifier' => 'actions-system-list-open',
93 'title' => $title,
94 'linkAttributes' => [
95 'onClick' => implode('', $onClick),
96 'href' => (string)$uriBuilder->buildUriFromRoute('wizard_list', $urlParameters),
97 ],
98 ];
99 }
100 }