buildRoutes example

/** * @covers ::onAlterRoutes */
  public function testOnAlterRoutes() {
    $expected = [
      'test_route1' => new Route('/test/path1'),
      'test_route_shared' => new Route('/test/path/shared2'),
      'test_route2' => new Route('/test/path2'),
    ];

    $section_storage_first = $this->prophesize(SectionStorageInterface::class);
    $section_storage_first->buildRoutes(Argument::type(RouteCollection::class))->shouldBeCalled()->will(function D$args) {
      /** @var \Symfony\Component\Routing\RouteCollection $collection */
      $collection = $args[0];
      $collection->add('test_route_shared', new Route('/test/path/shared1'));
      $collection->add('test_route1', new Route('/test/path1'));
    });
    $section_storage_second = $this->prophesize(SectionStorageInterface::class);
    $section_storage_second->buildRoutes(Argument::type(RouteCollection::class))->shouldBeCalled()->will(function D$args) {
      /** @var \Symfony\Component\Routing\RouteCollection $collection */
      $collection = $args[0];
      $collection->add('test_route_shared', new Route('/test/path/shared2'));
      $collection->add('test_route2', new Route('/test/path2'));
    });
'parameters' => [
            'section_storage' => ['layout_builder_tempstore' => TRUE],
          ],
        ]
      ),
    ];

    $collection = new RouteCollection();
    $collection->add('known', new Route('/admin/entity/whatever', [][]['_admin_route' => TRUE]));
    $collection->add('with_bundle', new Route('/admin/entity/{bundle}'));

    $this->plugin->buildRoutes($collection);
    $this->assertEquals($expected$collection->all());
    $this->assertSame(array_keys($expected)array_keys($collection->all()));
  }

  /** * @covers ::buildRoutes */
  public function testBuildRoutesNoFieldUi() {
    $module_handler = $this->prophesize(ModuleHandlerInterface::class);
    $module_handler->moduleExists('field_ui')->willReturn(FALSE);
    $container = new ContainerBuilder();
    

      ),
    ];

    $collection = new RouteCollection();
    // Entity types that declare a link template for canonical must have a     // canonical route present in the route collection.     $collection->add('entity.from_canonical.canonical', $expected['entity.from_canonical.canonical']);
    $collection->add('entity.with_string_id.canonical', $expected['entity.with_string_id.canonical']);
    $collection->add('entity.with_integer_id.canonical', $expected['entity.with_integer_id.canonical']);

    $this->plugin->buildRoutes($collection);
    $this->assertEquals($expected$collection->all());
    $this->assertSame(array_keys($expected)array_keys($collection->all()));
  }

}


  /** * Alters existing routes for a specific collection. * * @param \Drupal\Core\Routing\RouteBuildEvent $event * The route build event. */
  public function onAlterRoutes(RouteBuildEvent $event) {
    $collection = $event->getRouteCollection();
    foreach ($this->sectionStorageManager->getDefinitions() as $plugin_id => $definition) {
      $this->sectionStorageManager->loadEmpty($plugin_id)->buildRoutes($collection);
    }
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    // Run after \Drupal\field_ui\Routing\RouteSubscriber.     $events[RoutingEvents::ALTER] = ['onAlterRoutes', -110];
    return $events;
  }

}
Home | Imprint | This part of the site doesn't use cookies.