Skip to content

Instantly share code, notes, and snippets.

@EclipseGc
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EclipseGc/e07076423bee5ffd5df1 to your computer and use it in GitHub Desktop.
Save EclipseGc/e07076423bee5ffd5df1 to your computer and use it in GitHub Desktop.
<?php
/**
* links = {
* "canonical" = "/node/{node}",
* "delete-form" = "/node/{node}/delete",
* "edit-form" = "/node/{node}/edit",
* "version-history" = "/node/{node}/revision/{revision}",
* "admin-form" = "/admin/structure/whatever..."
* }
*/
<?php
class MySubscriber implements EventSubscriberInterface {
public function __construct(EntityManager $entity_manager, LinkRelationshipManagerInterface $manager) {
$this->entity_manager = $entity_manager;
$this->manager = $manager;
}
public static function getSubscribedEvents() {
return array(
Routing::DYNAMIC => array(array('onDynamicRoute')),
);
}
public function onDynamicRoute() {
$collection = new RouteCollection();
foreach($this->entity_manager->getDefinitions() as $entity_info) {
foreach ($entity_info['links'] as $relationship => $pattern) {
$relationship_plugin = $this->manager->createInstance($relationship);
$route = $relationships_plugin->getRoute($pattern, $entity_info);
}
$collection->addRoute($route);
}
return $collection;
}
}
@Crell
Copy link

Crell commented May 8, 2014

  public function onDynamicRoute($collection) {
    foreach($this->entity_manager->getDefinitions() as $entity_info) {
      foreach ($entity_info['links'] as $relationship => $pattern) {
        $relationship_plugin = $this->manager->createInstance($relationship);
        $route = $relationships_plugin->getRoute($pattern, $entity_info);
      }
      $route_name = $entity_info['name'] . '.' . $relationship;
      if (! $collection->has($route_name)) {
        $collection->addRoute($route_name, $route);
      }

    }
    return $collection;
  }

@EclipseGc
Copy link
Author

That seems like a sane addition to the overall code and gives us the ability to overwrite route defaults by simply writing them at the *.routing.yml level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment