AbstractItemProvider.php 4.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace TYPO3\CMS\Backend\Form\FormDataProvider;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Lang\LanguageService;

/**
 * Contains methods used by Data providers that handle elements
 * with single items like select, radio and some more.
 */
abstract class AbstractItemProvider {

	/**
	 * Resolve "itemProcFunc" of elements.
	 *
	 * @param array $result Main result array
	 * @param string $fieldName Field name to handle item list for
	 * @param array $items Existing items array
	 * @return array New list of item elements
	 */
	protected function resolveItemProcessorFunction(array $result, $fieldName, array $items) {
		$table = $result['tableName'];
		$config = $result['processedTca']['columns'][$fieldName]['config'];

		$pageTsProcessorParameters = NULL;
		if (!empty($result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['itemsProcFunc.'])) {
			$pageTsProcessorParameters = $result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['itemsProcFunc.'];
		}
		$processorParameters = [
			// Function manipulates $items directly and return nothing
			'items' => &$items,
			'config' => $config,
			'TSconfig' => $pageTsProcessorParameters,
			'table' => $table,
			'row' => $result['databaseRow'],
			'field' => $fieldName,
		];

		try {
			GeneralUtility::callUserFunction($config['itemsProcFunc'], $processorParameters, $this);
		} catch (\Exception $exception) {
			// The itemsProcFunc method may throw an exception, create a flash message if so
			$languageService = $this->getLanguageService();
			$fieldLabel = $fieldName;
			if (!empty($result['processedTca']['columns'][$fieldName]['label'])) {
				$fieldLabel = $languageService->sL($result['processedTca']['columns'][$fieldName]['label']);
			}
			$message = sprintf(
				$languageService->sL('LLL:EXT:lang/locallang_core.xlf:error.items_proc_func_error'),
				$fieldLabel,
				$exception->getMessage()
			);
			/** @var FlashMessage $flashMessage */
			$flashMessage = GeneralUtility::makeInstance(
				FlashMessage::class,
71
				$message,
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
				'',
				FlashMessage::ERROR,
				TRUE
			);
			/** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
			$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
			$defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
			$defaultFlashMessageQueue->enqueue($flashMessage);
		}

		return $items;
	}

	/**
	 * PageTsConfig addItems:
	 *
	 * TCEFORMS.aTable.aField[.types][.aType].addItems.aValue = aLabel,
	 * with type specific options merged by pageTsConfigMerged already
	 *
	 * @param array $result result array
	 * @param string $fieldName Current handle field name
	 * @param array $items Incoming items
	 * @return array Modified item array
	 */
	protected function addItemsFromPageTsConfig(array $result, $fieldName, array $items) {
		$table = $result['tableName'];
		if (!empty($result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'])
			&& is_array($result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'])
		) {
			$addItemsArray = $result['pageTsConfigMerged']['TCEFORM.'][$table . '.'][$fieldName . '.']['addItems.'];
			foreach ($addItemsArray as $value => $label) {
				// If the value ends with a dot, it is a subelement like "34.icon = mylabel.png", skip it
				if (substr($value, -1) === '.') {
					continue;
				}
				// Check if value "34 = mylabel" also has a "34.icon = myImage.png"
				$icon = NULL;
				if (isset($addItemsArray[$value . '.'])
					&& is_array($addItemsArray[$value . '.'])
					&& !empty($addItemsArray[$value . '.']['icon'])
				) {
					$icon = $addItemsArray[$value . '.']['icon'];
				}
				$items[] = array($label, $value, $icon);
			}
		}
		return $items;
	}

	/**
	 * @return LanguageService
	 */
	protected function getLanguageService() {
		return $GLOBALS['LANG'];
	}

}