Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

RNG routes fail if the entity routing parameter is named 'event' due to default route parameters. #168

Open
if-owen opened this issue Jun 7, 2018 · 0 comments

Comments

@if-owen
Copy link

if-owen commented Jun 7, 2018

I have created a custom entity type called Event which I wish to designate as an RNG event type. I have set up the configuration as required and I can see Event and Register tabs when viewing an entity of type Event. Great.

However, when I attempt to visit the Event or Register tabs I get presented with a 404. I have done extensive debugging on why this may be. I have checked out all the registered routes using both Drupal Console and this snippet:

$rp = \Drupal::service('router.route_provider');
$routes = $rp->getAllRoutes();

I could see the paths in there that I would expect to see such as this one for .../event

["rng.event.event.event"]=>
  object(Symfony\Component\Routing\Route)#8835 (9) {
    ["path":"Symfony\Component\Routing\Route":private]=>
    string(28) "/events/series/{event}/event"
    ["host":"Symfony\Component\Routing\Route":private]=>
    string(0) ""
    ["schemes":"Symfony\Component\Routing\Route":private]=>
    array(0) {
    }
    ["methods":"Symfony\Component\Routing\Route":private]=>
    array(0) {
    }
    ["defaults":"Symfony\Component\Routing\Route":private]=>
    array(3) {
      ["_form"]=>
      string(34) "\Drupal\rng\Form\EventSettingsForm"
      ["_title"]=>
      string(12) "Manage event"
      ["event"]=>
      string(5) "event"
    }
    ["requirements":"Symfony\Component\Routing\Route":private]=>
    array(2) {
      ["_entity_access"]=>
      string(18) "event.manage event"
      ["_entity_is_event"]=>
      string(4) "TRUE"
    }
    ["options":"Symfony\Component\Routing\Route":private]=>
    array(3) {
      ["compiler_class"]=>
      string(39) "Symfony\Component\Routing\RouteCompiler"
      ["parameters"]=>
      array(1) {
        ["event"]=>
        array(1) {
          ["type"]=>
          string(12) "entity:event"
        }
      }
      ["_admin_route"]=>
      string(4) "TRUE"
    }
    ["condition":"Symfony\Component\Routing\Route":private]=>
    string(0) ""
    ["compiled":"Symfony\Component\Routing\Route":private]=>
    NULL
  }

So the routes exist, but I cannot access them. So I then began to dig through the Drupal routing and I checked the router database table and sure enough I spot the following:

rng.event.event.event /events/series/{event}/event /events/series/event

Clearly the pattern_outline field (3rd field above) is incorrect. It should be /events/series/%/event. And that can be confirmed by verifying the path ancestors from RouteProvider:

array(14) {
  [0]=>
  string(22) "/events/series/1/event"
  [1]=>
  string(18) "/events/series/1/%"
  [2]=>
  string(22) "/events/series/%/event"
  [3]=>
  string(18) "/events/series/%/%"
  [4]=>
  string(17) "/events/%/1/event"
  [5]=>
  string(13) "/events/%/1/%"
  [6]=>
  string(17) "/events/%/%/event"
  [7]=>
  string(13) "/events/%/%/%"
  [8]=>
  string(16) "/events/series/1"
  [9]=>
  string(16) "/events/series/%"
  [10]=>
  string(11) "/events/%/1"
  [11]=>
  string(14) "/events/series"
  [12]=>
  string(9) "/events/%"
  [13]=>
  string(7) "/events"
}

Eventually I managed to figure out that the way the way that RNG defines RouteSubscriber::alterRoutes() it is defining the default parameter values are below:

array(
  '_form' => '\Drupal\rng\Form\EventSettingsForm',
  '_title' => 'Manage event',
  // Tell controller which parameter the event entity is stored.
  'event' => $entity_type,
),

In this case, the killer is event because \Drupal\Core\Routing\RouteCompiler::getPathWithoutDefaults() then strips out the defaults from the path in this case it would strip out /{_form} , /{_title}, and critically /{event} which is how we end up with /events/series/event instead of /events/series/%/event.

So because my entity type is named event and the routing parameter is called event in my custom routes this breaks all RNG paths.

Given the likelihood of developers naming an entity type used for events as event this seems like it could have pretty dire consequences for people other than myself.

Could the default parameter be named something else - like rng_event ?

@if-owen if-owen changed the title RNG routes fail if the entity name is 'Event' due to default route parameters. RNG routes fail if the entity routing parameter is name 'event' due to default route parameters. Jun 7, 2018
@if-owen if-owen changed the title RNG routes fail if the entity routing parameter is name 'event' due to default route parameters. RNG routes fail if the entity routing parameter is named 'event' due to default route parameters. Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant