1

I try to understand how Drupal Core rebuild the route automatically after route created programaticaly.

here is one example that i tried: I'm installing External Entities Module. this module will allow you to create entity and generate its route automatically.

question: how is this possible? what trigger Drupal core to rebuild the route?

i did stack trace on entity form-save action as follow: (to understand what's triggered route rebuild)

  • KernelDesctructionSubscriber.php line 48 --> public function onKernelTerminate(PostResponseEvent $event)
  • ContainerAwareEventDispacther.php line 111 --> $definition['callable']($event, $event_name, $this);
  • HttpKernel.php Line 84 --> $this->dispatcher->dispatch(KernelEvents::TERMINATE, new PostResponseEvent($this, $request, $response));
  • StackedHttpKernel.Php Line 32 --> $kernel->terminate($request, $response);
  • Drupal Kernel Line 634 --> $this->getHttpKernel()->terminate($request, $response);
  • index.php Line 22 --> $kernel->terminate($request, $response);

if you see KernelDestructionSubscriber in specific on: onKernelTerminate

public function onKernelTerminate(PostResponseEvent $event) {
    foreach ($this->services as $id) {
      // Check if the service was initialized during this request, destruction
      // is not necessary if the service was not used.
      if ($this->container->initialized($id)) {
        $service = $this->container->get($id);
        $service->destruct();
      }
    }
  }

when the ID equal to drupal.proxy_original_service.router.builder then it goes to: container.php : initialized method below:

  public function initialized($id) {
    if (isset($this->aliases[$id])) {
      $id = $this->aliases[$id];
    }

    return isset($this->services[$id]) || array_key_exists($id, $this->services);
  }

it will return TRUE because $this->services[$id] is set

and then it will trigger: $service->destruct() in onKernelTerminate then rebuild the route.

the question is where in the External Entities code that will make $this->services[$id] is set then trigger rebuild route?

i've been spending quite sometime and still unable to crack this...

Thanks in advance for the help

  • It might be easier to ask and answer what you are actually trying to achieve and explain why you need to know this. – Berdir Apr 9 '17 at 8:05
  • @Berdir, thanks for replying, to be honest, it is more into understanding how the code works inside Drupal when you create route programatically,, so then i can write the code 'properly'. And the in the core code seems like there is a way for you when you create a route programmatically it will refresh the route automatically that i like to understand how... – AnD Apr 10 '17 at 4:20
  • Then you should ask that, explain your use case and what you want to do, that will be a lot easier to answer and will help you more – Berdir Apr 10 '17 at 7:56
  • Agree with you @Berdir, I have edited my question, i hope that's clear - my objective is to understand how Drupal Core rebuild the route automatically after route created programaticaly. – AnD Apr 10 '17 at 11:11
0

I can't see anything in the module doing it, but this is done through the router.builder service.

Either rebuild immediately, with \Drupal::service('router.builder')->rebuild(); or tell it to rebuild at the end of the request, which is \Drupal::service('router.builder')->setRebuildNeeded()

Based on what you posted, it looks to me that you went through all the boring, generic stuff and stopped right before if would have gotten interesting, in that services rebuildIfNeeded() method. which checks the rebuildNeeded flag and rebuilds if TRUE.

So, you don't have to do anything to have that method called, that happens automatically if the service got invoked, you just have to make sure that flag is set.

  • Thanks @Berdir, do you mind to elaborate more on: "if the service got invoked, you just have to make sure that flag is set" ? which and how service get invoked and which (and how) 'that flag' get set? – AnD Apr 10 '17 at 18:01
  • That's exactly what I said in the second paragraph? – Berdir Apr 10 '17 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.