summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/system/src/Routing/MenuLinksetRoutes.php
blob: 72d0c2910193b751fb2b7a165a46de0bda18cf28 (plain) (blame)
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
71
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
<?php

namespace Drupal\system\Routing;

use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RouteBuilderInterface;
use Symfony\Component\Routing\RouteCollection;

/**
 * Dynamically defines routes for menu linkset endpoints.
 */
class MenuLinksetRoutes extends RouteSubscriberBase implements ContainerInjectionInterface {

  /**
   * An array of enabled authentication provider IDs.
   *
   * @var string[]
   */
  protected readonly array $providerIds;

  /**
   * EventSubscriber constructor.
   *
   * @param string[] $authenticationProviders
   *   An array of authentication providers, keyed by ID.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   * @param \Drupal\Core\Routing\RouteBuilderInterface $routeBuilder
   *   The route builder.
   */
  public function __construct(array $authenticationProviders, protected readonly ConfigFactoryInterface $configFactory, protected readonly RouteBuilderInterface $routeBuilder) {
    $this->providerIds = array_keys($authenticationProviders);
  }

  /**
   * Alter routes.
   *
   * If the endpoint is configured to be enabled, dynamically enable all
   * authentication providers on this module's routes since they cannot be known
   * in advance.
   *
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   A collection of routes.
   */
  public function alterRoutes(RouteCollection $collection) {
    if ($this->configFactory->get('system.feature_flags')->get('linkset_endpoint')) {
      $collection->get('system.menu.linkset')->setOption('_auth', $this->providerIds);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function onConfigSave(ConfigCrudEvent $event) {
    $saved_config = $event->getConfig();
    if ($saved_config->getName() === 'system.feature_flags' && $event->isChanged('linkset_endpoint')) {
      $this->routeBuilder->setRebuildNeeded();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    $events = parent::getSubscribedEvents();
    // Run after the route alter event subscriber.
    $events[ConfigEvents::SAVE][] = ['onConfigSave', 0];
    return $events;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->getParameter('authentication_providers'),
      $container->get('config.factory'),
      $container->get('router.builder')
    );
  }

  /**
   * Returns an array of route objects.
   *
   * @return \Symfony\Component\Routing\Route[]
   *   An array of route objects.
   */
  public function routes() {
    $routes = [];

    // Only enable linkset routes if the related config option is enabled.
    if ($this->configFactory->get('system.feature_flags')->get('linkset_endpoint')) {
      $routes['system.menu.linkset'] = new Route(
        '/system/menu/{menu}/linkset',
        [
          '_controller' => 'Drupal\system\Controller\LinksetController::process',
        ],
        [
          '_access' => 'TRUE',
          '_format' => 'json',
        ],
        [
          'parameters' => [
            'menu' => [
              'type' => 'entity:menu',
            ],
          ],
        ]
      );
    }
    return $routes;
  }

}