1<?php
2declare(strict_types = 1);
3namespace 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
18use TYPO3\CMS\Backend\Form\AbstractNode;
19use TYPO3\CMS\Core\Utility\GeneralUtility;
20use TYPO3\CMS\Core\Utility\StringUtility;
21
22/**
23 * Renders the icon with link parameters to add a new record,
24 * typically used for single elements of type=group or type=select.
25 */
26class AddRecord extends AbstractNode
27{
28    /**
29     * Add button control
30     *
31     * @return array As defined by FieldControl class
32     */
33    public function render(): array
34    {
35        $options = $this->data['renderData']['fieldControlOptions'];
36        $parameterArray = $this->data['parameterArray'];
37        $itemName = $parameterArray['itemFormElName'];
38
39        // Handle options and fallback
40        $title = $options['title'] ?? 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.createNew';
41        $setValue = $options['setValue'] ?? 'append';
42
43        $table = '';
44        if (isset($options['table'])) {
45            // Table given in options - use it
46            $table = $options['table'];
47        } elseif ($parameterArray['fieldConf']['config']['type'] === 'group'
48            && isset($parameterArray['fieldConf']['config']['internal_type'])
49            && $parameterArray['fieldConf']['config']['internal_type'] === 'db'
50            && !empty($parameterArray['fieldConf']['config']['allowed'])
51        ) {
52            // Use first table from allowed list if specific table is not set in options
53            $allowedTables = GeneralUtility::trimExplode(',', $parameterArray['fieldConf']['config']['allowed'], true);
54            $table = array_pop($allowedTables);
55        } elseif ($parameterArray['fieldConf']['config']['type'] === 'select'
56            && !empty($parameterArray['fieldConf']['config']['foreign_table'])
57        ) {
58            // Use foreign_table if given for type=select
59            $table = $parameterArray['fieldConf']['config']['foreign_table'];
60        }
61        if (empty($table)) {
62            // Still no table - this element can not handle the add control.
63            return [];
64        }
65
66        // Target pid of new records is current pid by default
67        $pid = $this->data['effectivePid'];
68        if (isset($options['pid'])) {
69            // pid configured in options - use it
70            $pid = $options['pid'];
71        } elseif (
72            isset($GLOBALS['TCA'][$table]['ctrl']['rootLevel'])
73            && (int)$GLOBALS['TCA'][$table]['ctrl']['rootLevel'] === 1
74        ) {
75            // Target table can only exist on root level - set 0 as pid
76            $pid = 0;
77        }
78
79        $prefixOfFormElName = 'data[' . $this->data['tableName'] . '][' . $this->data['databaseRow']['uid'] . '][' . $this->data['fieldName'] . ']';
80        $flexFormPath = '';
81        if (GeneralUtility::isFirstPartOfStr($itemName, $prefixOfFormElName)) {
82            $flexFormPath = str_replace('][', '/', substr($itemName, strlen($prefixOfFormElName) + 1, -1));
83        }
84
85        $urlParameters = [
86            'P' => [
87                'params' => [
88                    'table' => $table,
89                    'pid' => $pid,
90                    'setValue' => $setValue,
91                ],
92                'table' => $this->data['tableName'],
93                'field' => $this->data['fieldName'],
94                'uid' => $this->data['databaseRow']['uid'],
95                'flexFormPath' => $flexFormPath,
96                'returnUrl' => $this->data['returnUrl'],
97            ],
98        ];
99
100        $id = StringUtility::getUniqueId('t3js-formengine-fieldcontrol-');
101
102        /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
103        $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
104        return [
105            'iconIdentifier' => 'actions-add',
106            'title' => $title,
107            'linkAttributes' => [
108                'id' => htmlspecialchars($id),
109                'href' => (string)$uriBuilder->buildUriFromRoute('wizard_add', $urlParameters),
110            ],
111            'requireJsModules' => [
112                ['TYPO3/CMS/Backend/FormEngine/FieldControl/AddRecord' => 'function(FieldControl) {new FieldControl(' . GeneralUtility::quoteJSvalue('#' . $id) . ');}'],
113            ],
114        ];
115    }
116}
117