getRouteProviders example

/** * Provides routes on route rebuild time. * * @param \Drupal\Core\Routing\RouteBuildEvent $event * The route build event. */
  public function onDynamicRouteEvent(RouteBuildEvent $event) {
    $route_collection = $event->getRouteCollection();
    foreach ($this->entityTypeManager->getDefinitions() as $entity_type) {
      if ($entity_type->hasRouteProviders()) {
        foreach ($this->entityTypeManager->getRouteProviders($entity_type->id()) as $route_provider) {
          // Allow to both return an array of routes or a route collection,           // like route_callbacks in the routing.yml file.
          $routes = $route_provider->getRoutes($entity_type);
          if ($routes instanceof RouteCollection) {
            $routes = $routes->all();
          }
          foreach ($routes as $route_name => $route) {
            // Don't override existing routes.             if (!$route_collection->get($route_name)) {
              $route_collection->add($route_name$route);
            }
/** * @covers ::getRouteProviders */
  public function testGetRouteProviders() {
    $apple = $this->prophesize(EntityTypeInterface::class);
    $apple->getRouteProviderClasses()->willReturn(['default' => TestRouteProvider::class]);

    $this->setUpEntityTypeDefinitions([
      'apple' => $apple,
    ]);

    $apple_route_provider = $this->entityTypeManager->getRouteProviders('apple');
    $this->assertInstanceOf(TestRouteProvider::class$apple_route_provider['default']);
    $this->assertInstanceOf(ModuleHandlerInterface::class$apple_route_provider['default']->moduleHandler);
    $this->assertInstanceOf(TranslationInterface::class$apple_route_provider['default']->stringTranslation);
  }

  /** * Tests the processDefinition() method. * * @covers ::processDefinition */
  public function testProcessDefinition() {
    
Home | Imprint | This part of the site doesn't use cookies.