[FEATURE] Allow deactivation of FormDataProviders
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataGroup / OrderedProviderList.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Form\FormDataGroup;
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
18 use TYPO3\CMS\Backend\Form\FormDataGroupInterface;
19 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
20 use TYPO3\CMS\Core\Service\DependencyOrderingService;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Calls provider with dependencies specified given by setter
25 *
26 * This group is used to call a list of providers in order by specified
27 * dependencies before/depends.
28 */
29 class OrderedProviderList implements FormDataGroupInterface
30 {
31 /**
32 * @var FormDataProviderInterface[]
33 */
34 protected $providerList = [];
35
36 /**
37 * Compile form data
38 *
39 * @param array $result Initialized result array
40 * @return array Result filled with data
41 * @throws \UnexpectedValueException
42 */
43 public function compile(array $result): array
44 {
45 $orderingService = GeneralUtility::makeInstance(DependencyOrderingService::class);
46 $orderedDataProvider = $orderingService->orderByDependencies($this->providerList, 'before', 'depends');
47
48 foreach ($orderedDataProvider as $providerClassName => $providerConfig) {
49 if (isset($providerConfig['disabled']) && $providerConfig['disabled'] === true) {
50 // Skip this data provider if disabled by configuration
51 continue;
52 }
53
54 /** @var FormDataProviderInterface $provider */
55 $provider = GeneralUtility::makeInstance($providerClassName);
56
57 if (!$provider instanceof FormDataProviderInterface) {
58 throw new \UnexpectedValueException(
59 'Data provider ' . $providerClassName . ' must implement FormDataProviderInterface',
60 1485299408
61 );
62 }
63
64 $result = $provider->addData($result);
65 }
66
67 return $result;
68 }
69
70 /**
71 * Set list of providers to be called
72 *
73 * The dependencies of a provider are specified as:
74 *
75 * FormDataProvider::class => [
76 * 'before' => [AnotherFormDataProvider::class]
77 * 'depends' => [YetAnotherFormDataProvider::class]
78 * ]
79 *
80 * @param array $list Given list of Provider class names
81 * @see \TYPO3\CMS\Core\Service\DependencyOrderingService
82 */
83 public function setProviderList(array $list)
84 {
85 $this->providerList = $list;
86 }
87 }