[BUGFIX] Remove superflouos encoding in error message
[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 $table = $result['tableName'];
38 $config = $result['processedTca']['columns'][$fieldName]['config'];
39
40 $pageTsProcessorParameters = NULL;
41 if (!empty($result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['itemsProcFunc.'])) {
42 $pageTsProcessorParameters = $result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['itemsProcFunc.'];
43 }
44 $processorParameters = [
45 // Function manipulates $items directly and return nothing
46 'items' => &$items,
47 'config' => $config,
48 'TSconfig' => $pageTsProcessorParameters,
49 'table' => $table,
50 'row' => $result['databaseRow'],
51 'field' => $fieldName,
52 ];
53
54 try {
55 GeneralUtility::callUserFunction($config['itemsProcFunc'], $processorParameters, $this);
56 } catch (\Exception $exception) {
57 // The itemsProcFunc method may throw an exception, create a flash message if so
58 $languageService = $this->getLanguageService();
59 $fieldLabel = $fieldName;
60 if (!empty($result['processedTca']['columns'][$fieldName]['label'])) {
61 $fieldLabel = $languageService->sL($result['processedTca']['columns'][$fieldName]['label']);
62 }
63 $message = sprintf(
64 $languageService->sL('LLL:EXT:lang/locallang_core.xlf:error.items_proc_func_error'),
65 $fieldLabel,
66 $exception->getMessage()
67 );
68 /** @var FlashMessage $flashMessage */
69 $flashMessage = GeneralUtility::makeInstance(
70 FlashMessage::class,
71 $message,
72 '',
73 FlashMessage::ERROR,
74 TRUE
75 );
76 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
77 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
78 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
79 $defaultFlashMessageQueue->enqueue($flashMessage);
80 }
81
82 return $items;
83 }
84
85 /**
86 * PageTsConfig addItems:
87 *
88 * TCEFORMS.aTable.aField[.types][.aType].addItems.aValue = aLabel,
89 * with type specific options merged by pageTsConfigMerged already
90 *
91 * @param array $result result array
92 * @param string $fieldName Current handle field name
93 * @param array $items Incoming items
94 * @return array Modified item array
95 */
96 protected function addItemsFromPageTsConfig(array $result, $fieldName, array $items) {
97 $table = $result['tableName'];
98 if (!empty($result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'])
99 && is_array($result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'])
100 ) {
101 $addItemsArray = $result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'];
102 foreach ($addItemsArray as $value => $label) {
103 // If the value ends with a dot, it is a subelement like "34.icon = mylabel.png", skip it
104 if (substr($value, -1) === '.') {
105 continue;
106 }
107 // Check if value "34 = mylabel" also has a "34.icon = myImage.png"
108 $icon = NULL;
109 if (isset($addItemsArray[$value . '.'])
110 && is_array($addItemsArray[$value . '.'])
111 && !empty($addItemsArray[$value . '.']['icon'])
112 ) {
113 $icon = $addItemsArray[$value . '.']['icon'];
114 }
115 $items[] = array($label, $value, $icon);
116 }
117 }
118 return $items;
119 }
120
121 /**
122 * @return LanguageService
123 */
124 protected function getLanguageService() {
125 return $GLOBALS['LANG'];
126 }
127
128 }