‪TYPO3CMS  ‪main
ElementBrowser.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 
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
25 
31 {
32  public function ‪__construct(
33  private readonly ‪InlineStackProcessor $inlineStackProcessor,
34  ) {}
35 
41  public function ‪render(): array
42  {
43  $table = $this->data['tableName'];
44  $fieldName = $this->data['fieldName'];
45  $options = $this->data['renderData']['fieldControlOptions'];
46  $parameterArray = $this->data['parameterArray'];
47  $elementName = $parameterArray['itemFormElName'];
48  $config = $parameterArray['fieldConf']['config'];
49  $type = $config['type'];
50 
51  // Remove any white-spaces from the allowed extension lists
52  $allowed = implode(',', ‪GeneralUtility::trimExplode(',', (string)($config['allowed'] ?? ''), true));
53 
54  if (isset($config['readOnly']) && $config['readOnly']) {
55  return [];
56  }
57 
58  if ($options['title'] ?? false) {
59  $title = $options['title'];
60  } elseif ($type === 'group') {
61  $title = 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.browse_db';
62  } elseif ($type === 'folder') {
63  $title = 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.browse_folder';
64  } else {
65  // FieldControl requires to provide a title -> Set default if non is given and custom TCA config is used
66  $title = 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.browse_elements';
67  }
68 
69  // Check against inline uniqueness - Create some onclick js for delete control and element browser
70  // to override record selection in some FAL scenarios - See 'appearance' docs of group element
71  $this->inlineStackProcessor->initializeByGivenStructure($this->data['inlineStructure']);
72  $objectPrefix = '';
73  if (($this->data['isInlineChild'] ?? false)
74  && ($this->data['inlineParentUid'] ?? false)
75  && ($this->data['inlineParentConfig']['foreign_table'] ?? false) === $table
76  && ($this->data['inlineParentConfig']['foreign_unique'] ?? false) === $fieldName
77  ) {
78  $objectPrefix = $this->inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->data['inlineFirstPid']) . '-' . $table;
79  }
80 
81  if ($type === 'group') {
82  if (($this->data['inlineParentConfig']['type'] ?? '') === 'file') {
83  $elementBrowserType = 'file';
84  // Remove any white-spaces from the allowed extension lists
85  $allowed = implode(',', ‪GeneralUtility::trimExplode(',', (string)($this->data['inlineParentConfig']['allowed'] ?? ''), true));
86  } else {
87  $elementBrowserType = 'db';
88  }
89  } else {
90  $elementBrowserType = 'folder';
91  }
92 
93  // Initialize link attributes
94  $linkAttributes = [
95  'class' => 't3js-element-browser',
96  'data-mode' => $elementBrowserType,
97  'data-params' => $elementName . '|||' . $allowed . '|' . $objectPrefix,
98  ];
99 
100  // Add the default entry point - if found
101  $linkAttributes = $this->‪addEntryPoint($table, $fieldName, $config, $linkAttributes);
102 
103  return [
104  'iconIdentifier' => 'actions-insert-record',
105  'title' => $title,
106  'linkAttributes' => $linkAttributes,
107  ];
108  }
109 
114  protected function ‪addEntryPoint(string $table, string $fieldName, array $fieldConfig, array $linkAttributes): array
115  {
116  if (!isset($fieldConfig['elementBrowserEntryPoints']) || !is_array($fieldConfig['elementBrowserEntryPoints'])) {
117  // Early return in case no entry points are defined
118  return $linkAttributes;
119  }
120 
121  // Fetch the configured default entry point (which might be a marker)
122  $entryPoint = (string)($fieldConfig['elementBrowserEntryPoints']['_default'] ?? '');
123 
124  // In case no default entry point is given, check if we deal with type=db and only one allowed table
125  if ($entryPoint === '') {
126  if ($fieldConfig['type'] === 'folder') {
127  // Return for type folder as this requires the "_default" key to be set
128  return $linkAttributes;
129  }
130  // Check for the allowed tables, if only one table is allowed check if an entry point is defined for it
131  $allowed = ‪GeneralUtility::trimExplode(',', $fieldConfig['allowed'] ?? '', true);
132  if (count($allowed) === 1 && isset($fieldConfig['elementBrowserEntryPoints'][$allowed[0]])) {
133  // Use the entry point for the single table as default
134  $entryPoint = (string)$fieldConfig['elementBrowserEntryPoints'][$allowed[0]];
135  }
136  if ($entryPoint === '') {
137  // Return if still empty
138  return $linkAttributes;
139  }
140  }
141 
142  // Check and resolve possible marker
143  if (str_starts_with($entryPoint, '###') && str_ends_with($entryPoint, '###')) {
144  if ($entryPoint === '###CURRENT_PID###') {
145  // Use the current pid
146  $entryPoint = (string)$this->data['effectivePid'];
147  } elseif ($entryPoint === '###SITEROOT###' && ($this->data['site'] ?? null) instanceof ‪Site) {
148  // Use the root page id from the current site
149  $entryPoint = (string)$this->data['site']->getRootPageId();
150  } else {
151  // Check for special TSconfig marker
152  $TSconfig = BackendUtility::getTCEFORM_TSconfig($table, ['pid' => $this->data['effectivePid']]);
153  $keyword = substr($entryPoint, 3, -3);
154  if (str_starts_with($keyword, 'PAGE_TSCONFIG_')) {
155  $entryPoint = (string)($TSconfig[$fieldName][$keyword] ?? '');
156  } else {
157  $entryPoint = (string)($TSconfig['_' . $keyword] ?? '');
158  }
159  }
160  }
161 
162  // Add the entry point to the link attribute - if resolved
163  if ($entryPoint !== '') {
164  $linkAttributes['data-entry-point'] = $entryPoint;
165  }
166 
167  return $linkAttributes;
168  }
169 }
‪TYPO3\CMS\Backend\Form\FieldControl\ElementBrowser\addEntryPoint
‪addEntryPoint(string $table, string $fieldName, array $fieldConfig, array $linkAttributes)
Definition: ElementBrowser.php:114
‪TYPO3\CMS\Backend\Form\FieldControl\ElementBrowser\render
‪array render()
Definition: ElementBrowser.php:41
‪TYPO3\CMS\Backend\Form\FieldControl
Definition: AddRecord.php:18
‪TYPO3\CMS\Backend\Form\FieldControl\ElementBrowser\__construct
‪__construct(private readonly InlineStackProcessor $inlineStackProcessor,)
Definition: ElementBrowser.php:32
‪TYPO3\CMS\Backend\Form\FieldControl\ElementBrowser
Definition: ElementBrowser.php:31
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Backend\Form\InlineStackProcessor
Definition: InlineStackProcessor.php:32
‪TYPO3\CMS\Backend\Form\AbstractNode
Definition: AbstractNode.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822