0

I'm using the diff module for a project, this module has a method inside a Controller that I want to use in a service to reuse code. As this time I'm just calling the method like PluginRevisionController::getRevisionIds() but this give me the message:

Deprecated function: Non-static method Drupal\diff\Controller\PluginRevisionController::getRevisionIds() should not be called statically

So, I want to 'inject' if possible in some way the Controller (I really don't know if this is possible) inside the service to reuse the logic. There is a way to do this?

1
  • 4
    Bad diff module design, that method should've been in a service.
    – No Sssweat
    Jan 4, 2023 at 21:28

1 Answer 1

6

You could inject a ClassResolverInterface or ControllerResolverInterface into your service to load a controller instance.

From core.services.yml:

controller_resolver:
  class: Drupal\Core\Controller\ControllerResolver
  arguments: ['@psr7.http_message_factory', '@class_resolver']
class_resolver:
  class: Drupal\Core\DependencyInjection\ClassResolver
  calls:
    - [setContainer, ['@service_container']]

Both classes will return an instance, just the method names differ.

$controller = $this->classResolver->getInstanceFromDefinition(PluginRevisionController::class);
$controller = $this->controllerResolver->getControllerFromDefinition(PluginRevisionController::class);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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