ab39477783929de3e19968dd3b7f17d991cac3ae
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / AbstractItemProvider.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Messaging\FlashMessage;
18 use TYPO3\CMS\Core\Messaging\FlashMessageService;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Lang\LanguageService;
21
22 /**
23 * Contains methods used by Data providers that handle elements
24 * with single items like select, radio and some more.
25 */
26 abstract class AbstractItemProvider
27 {
28 /**
29 * Resolve "itemProcFunc" of elements.
30 *
31 * @param array $result Main result array
32 * @param string $fieldName Field name to handle item list for
33 * @param array $items Existing items array
34 * @return array New list of item elements
35 */
36 protected function resolveItemProcessorFunction(array $result, $fieldName, array $items)
37 {
38 $table = $result['tableName'];
39 $config = $result['processedTca']['columns'][$fieldName]['config'];
40
41 $pageTsProcessorParameters = null;
42 if (!empty($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['itemsProcFunc.'])) {
43 $pageTsProcessorParameters = $result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['itemsProcFunc.'];
44 }
45 $processorParameters = [
46 // Function manipulates $items directly and return nothing
47 'items' => &$items,
48 'config' => $config,
49 'TSconfig' => $pageTsProcessorParameters,
50 'table' => $table,
51 'row' => $result['databaseRow'],
52 'field' => $fieldName,
53 ];
54
55 try {
56 GeneralUtility::callUserFunction($config['itemsProcFunc'], $processorParameters, $this);
57 } catch (\Exception $exception) {
58 // The itemsProcFunc method may throw an exception, create a flash message if so
59 $languageService = $this->getLanguageService();
60 $fieldLabel = $fieldName;
61 if (!empty($result['processedTca']['columns'][$fieldName]['label'])) {
62 $fieldLabel = $languageService->sL($result['processedTca']['columns'][$fieldName]['label']);
63 }
64 $message = sprintf(
65 $languageService->sL('LLL:EXT:lang/locallang_core.xlf:error.items_proc_func_error'),
66 $fieldLabel,
67 $exception->getMessage()
68 );
69 /** @var FlashMessage $flashMessage */
70 $flashMessage = GeneralUtility::makeInstance(
71 FlashMessage::class,
72 $message,
73 '',
74 FlashMessage::ERROR,
75 true
76 );
77 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
78 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
79 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
80 $defaultFlashMessageQueue->enqueue($flashMessage);
81 }
82
83 return $items;
84 }
85
86 /**
87 * PageTsConfig addItems:
88 *
89 * TCEFORMS.aTable.aField[.types][.aType].addItems.aValue = aLabel,
90 * with type specific options merged by pageTsConfig already
91 *
92 * @param array $result result array
93 * @param string $fieldName Current handle field name
94 * @param array $items Incoming items
95 * @return array Modified item array
96 */
97 protected function addItemsFromPageTsConfig(array $result, $fieldName, array $items)
98 {
99 $table = $result['tableName'];
100 if (!empty($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'])
101 && is_array($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'])
102 ) {
103 $addItemsArray = $result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'];
104 foreach ($addItemsArray as $value => $label) {
105 // If the value ends with a dot, it is a subelement like "34.icon = mylabel.png", skip it
106 if (substr($value, -1) === '.') {
107 continue;
108 }
109 // Check if value "34 = mylabel" also has a "34.icon = myImage.png"
110 $icon = null;
111 if (isset($addItemsArray[$value . '.'])
112 && is_array($addItemsArray[$value . '.'])
113 && !empty($addItemsArray[$value . '.']['icon'])
114 ) {
115 $icon = $addItemsArray[$value . '.']['icon'];
116 }
117 $items[] = array($label, $value, $icon);
118 }
119 }
120 return $items;
121 }
122
123 /**
124 * @return LanguageService
125 */
126 protected function getLanguageService()
127 {
128 return $GLOBALS['LANG'];
129 }
130 }