‪TYPO3CMS  ‪main
WidgetRegistry.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 namespace ‪TYPO3\CMS\Dashboard;
19 
20 use Psr\Container\ContainerInterface;
21 use Psr\Http\Message\ServerRequestInterface;
28 
33 {
37  private ‪$widgets = [];
38 
42  private $widgetsPerWidgetGroup = [];
43 
44  public function __construct(protected readonly ContainerInterface $container) {}
45 
49  public function getAvailableWidgets(): array
50  {
51  return $this->‪checkPermissionOfWidgets($this->widgets);
52  }
53 
57  public function ‪getAllWidgets(): array
58  {
59  return ‪$this->widgets;
60  }
61 
65  public function ‪getAvailableWidget(ServerRequestInterface $request, string ‪$identifier): WidgetInterface
66  {
67  if (array_key_exists(‪$identifier, $this->getAvailableWidgets())) {
68  $widget = $this->container->get($this->widgets[‪$identifier]->getServiceName());
69  if ($widget instanceof RequestAwareWidgetInterface) {
70  $widget->setRequest($request);
71  }
72  return $widget;
73  }
74  throw new \InvalidArgumentException('Requested widget "' . ‪$identifier . '" does not exist.', 1584777201);
75  }
76 
80  public function ‪getAvailableWidgetsForWidgetGroup(string $widgetGroupIdentifier): array
81  {
82  if (!array_key_exists($widgetGroupIdentifier, $this->widgetsPerWidgetGroup)) {
83  return [];
84  }
85  return $this->‪checkPermissionOfWidgets($this->widgetsPerWidgetGroup[$widgetGroupIdentifier]);
86  }
87 
88  public function ‪registerWidget(string $serviceName): void
89  {
90  $widgetConfiguration = $this->container->get($serviceName);
91  $this->widgets[$widgetConfiguration->getIdentifier()] = $widgetConfiguration;
92  foreach ($widgetConfiguration->getGroupNames() as $groupIdentifier) {
93  $this->widgetsPerWidgetGroup = ‪ArrayUtility::setValueByPath(
94  $this->widgetsPerWidgetGroup,
95  $groupIdentifier . '/' . $widgetConfiguration->getIdentifier(),
96  $widgetConfiguration
97  );
98  }
99  }
100 
105  protected function ‪checkPermissionOfWidgets(array ‪$widgets): array
106  {
107  return array_filter(‪$widgets, function (‪$identifier) {
108  return $this->‪getBackendUser()->check('available_widgets', $identifier);
109  }, ARRAY_FILTER_USE_KEY);
110  }
111 
112  public function ‪widgetItemsProcFunc(array &$parameters): void
113  {
114  foreach ($this->widgets as $widget) {
115  $parameters['items'][] = [
116  'label' => $widget->getTitle(),
117  'value' => $widget->getIdentifier(),
118  'icon' => $widget->getIconIdentifier(),
119  'description' => $widget->getDescription(),
120  ];
121  }
122  }
123 
124  protected function ‪getBackendUser(): ‪BackendUserAuthentication
125  {
126  return ‪$GLOBALS['BE_USER'];
127  }
128 }
‪TYPO3\CMS\Dashboard\WidgetRegistry\checkPermissionOfWidgets
‪WidgetConfigurationInterface[] checkPermissionOfWidgets(array $widgets)
Definition: WidgetRegistry.php:103
‪TYPO3\CMS\Dashboard\Widgets\RequestAwareWidgetInterface
Definition: RequestAwareWidgetInterface.php:29
‪TYPO3\CMS\Dashboard\Widgets\WidgetInterface
Definition: WidgetInterface.php:26
‪TYPO3\CMS\Dashboard\WidgetRegistry\$widgets
‪array< string, WidgetConfigurationInterface > $widgets
Definition: WidgetRegistry.php:36
‪TYPO3\CMS\Dashboard\WidgetRegistry\getBackendUser
‪getBackendUser()
Definition: WidgetRegistry.php:122
‪TYPO3\CMS\Dashboard\WidgetRegistry\registerWidget
‪registerWidget(string $serviceName)
Definition: WidgetRegistry.php:86
‪TYPO3\CMS\Dashboard\WidgetRegistry
Definition: WidgetRegistry.php:33
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Dashboard\WidgetRegistry\getAvailableWidgetsForWidgetGroup
‪WidgetConfigurationInterface[] getAvailableWidgetsForWidgetGroup(string $widgetGroupIdentifier)
Definition: WidgetRegistry.php:78
‪TYPO3\CMS\Dashboard\WidgetRegistry\checkPermissionOfWidgets
‪array< string, $widgetsPerWidgetGroup=array();public __construct(protected readonly ContainerInterface $container) {} public WidgetConfigurationInterface[] function getAvailableWidgets():array { return $this-> checkPermissionOfWidgets($this->widgets)
‪TYPO3\CMS\Dashboard\WidgetRegistry\getAllWidgets
‪WidgetConfigurationInterface[] getAllWidgets()
Definition: WidgetRegistry.php:55
‪TYPO3\CMS\Core\Utility\ArrayUtility
Definition: ArrayUtility.php:26
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Dashboard\WidgetRegistry\getAvailableWidget
‪getAvailableWidget(ServerRequestInterface $request, string $identifier)
Definition: WidgetRegistry.php:63
‪TYPO3\CMS\Core\Utility\ArrayUtility\setValueByPath
‪static array setValueByPath(array $array, string|array|\ArrayAccess $path, mixed $value, string $delimiter='/')
Definition: ArrayUtility.php:261
‪TYPO3\CMS\Dashboard\Widgets\WidgetConfigurationInterface
Definition: WidgetConfigurationInterface.php:26
‪TYPO3\CMS\Dashboard\WidgetRegistry\widgetItemsProcFunc
‪widgetItemsProcFunc(array &$parameters)
Definition: WidgetRegistry.php:110
‪TYPO3\CMS\Dashboard
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37