0

I use a contrib controller that has a _custom_access function that I need to override.

I add a RouteSubscriber, and point the access check to my own controller. Now I want something like:

if ($condition) {
  return $new_access_decision;
}
return $original_controller->accessCheck()

so I can contrinue to rely on the original logic for most cases, and just override for a small subset of cases.

I don't know how to inject the original controller into my mine though, and if I try to add it as a paramter to the RouteSubscriber service, I get

has dependency on a non-existing service [original controller]

which I take to mean that controllers aren't services.

1

Inject the class_resolver as a regular service, and then call

$this->class_resolver->getInstanceFromDefinition('\Drupal\example\ExampleController');
2
  • Normally you don't need two instances if there is an OOP way extending the class and instantiating only the new class which can call the parent method if necessary. You need only two instances if they have different states.
    – 4k4
    Jan 14 at 9:45
  • Yeah, I guess a somewhat odd edge case. I suppose I could have solved the issue by extending the primart controller, but they are fairly different from one another, and it's inherited code that I'm not too keen on completely re-writing.
    – Letharion
    Jan 14 at 12:04

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.