Questions tagged [dependency-injection]

Use it for questions about implementing the software design pattern that implements inversion of control.

Filter by
Sorted by
Tagged with
7
votes
1answer
2k views

How to inject a service in another service?

I am building a custom service, a membership service. This service can be called from a controller. But what I don't seem to get working, is to use another service, the QueryFactory, to use in that ...
5
votes
1answer
2k views

Dependency Injection and the Url object

I'm making the leap from Drupal 7 to Drupal 8 and am still fuzzy on some core concepts, particularly relating to dependency injection (DI). I've read plenty of tutorials and have a grasp on DI with ...
5
votes
1answer
477 views

Can I get dependent services into a custom class without creating a service?

I've got a class hierarchy where I need to be able to instantiate different objects within the hierarchy. Instead of using global functions, I'd like to use injected services from the container via $...
1
vote
4answers
2k views

How can I get the form builder service using dependency injection?

How can I change form builder call in class in to dependency injection method? My current code: class CustomBlock extends BlockBase { /** * {@inheritdoc} */ public function build() { $...
5
votes
1answer
3k views

Problem with dependency injection (File::load and \Drupal::service)

In order to make a managed_file field permanent I did this in my module configuration form: <?php namespace Drupal\my_module\Form; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\...
1
vote
1answer
450 views

Dependency injection on module.install

How do I leverage dependency injection for drupal 8 when invoking my module.install->hook_install() mehtod? Or do I have to use \Drupal:: method at this point?
2
votes
0answers
99 views

Design help for serialized classes that need database access

I want to convert from \Drupal::database() to dependency injection. I'm not 100% certain how to move forward with a refactor I know I have to complete. Currently I have a series of custom classes ...
5
votes
1answer
5k views

Dependency inject a class with values from a config object?

Is it possible to DI dependencies in Drupal 8 but initialize it with values from a config object? For example, SoapClient requires specifying the WSDL when called. I have different WSDL URLs ...
6
votes
2answers
7k views

How to use dependency injection for Plugins/Blocks [\Drupal::formbuilder() and \Drupal::config()]?

I cannot get my head around this dependency injection theory. How are we supposed to get the below methods aligned with dependency injection? \Drupal::routeMatch()->getParameter('node'); \Drupal::...
0
votes
2answers
161 views

How do you inject dependencies in your classes from external packages?

My project has external PHP packages pulled in via Composer. I would like to inject one of the classes in that package as a dependency, instead of do new Class(); inside my Controller methods. The ...
1
vote
3answers
587 views

How to do dependency injection on complex classes like when extending WidgetBase

Since the Drupal sniffer whines at you if you call \Drupal::service() in your code, I have been trying to figure out how to use dependency injection on a widget I’ve been working on. Here’s what I’ve ...
8
votes
2answers
6k views

Dependency injection to load node data

When I checked the code I wrote with phpcs --standard=DrupalPractice /path/to/myController, I got the following warning. WARNING | Node::load calls should be avoided in classes, use dependency ...
2
votes
1answer
3k views

How to pass argument in service with dependency injection

In a custom module, I have a custom service that uses GuzzleHttp\Client service. mymodule.services.yml: services: mymodule.mymodule_call: class: Drupal\mymodule\MyModuleCall arguments: ['@...
2
votes
1answer
1k views

Drupal 8 \Drupal calls should be avoided in classes, use | | dependency injection instead

I used Drupal 8 and written a module. in my form page I called. \Drupal::service('library.discovery')->clearCachedDefinitions(); $cache = \Drupal::cache('discovery'); $cache->delete('...
6
votes
2answers
2k views

How do I use the global class \Drupal static methods to access services?

Is there a difference between calling \Drupal::service('config.factory') and \Drupal::configFactory()? Is it better to use the second one? Can \Drupal be used to do something else than calling ...
15
votes
1answer
6k views

How do I use dependency injection on \Drupal::moduleHandler() to a Controller?

I am using the following line in a controller class. \Drupal::moduleHandler()->alter('mymodule_myfunction', $plugin_items); Pareview.sh is giving me this error message. \Drupal calls ...
2
votes
2answers
173 views

Is it possible to define a FieldFormatter as a service

I need the 'language_manager' service inside a custom FieldFormatter. I'd prefer not to use \Drupal::service('language_manager'). Is there a special tag for FieldFormatters, or any other method to ...
0
votes
0answers
173 views

Issue when trying to create service from ContentEntity subclass and inject into another class

I have a class ApplicationForm that extends ContentEntity. In my other class ApplicationFormForm (Poorly named, I know) I extend ContentEntityForm which in turn inherits from EntityForm. In one of ...
1
vote
1answer
135 views

How to properly inject services into other services

I'm trying to figure out how to inject Drupal services into a custom service class. The class in question is my MenuService which builds an associative array of menu items which will be encoded to ...
2
votes
1answer
520 views

How do I find services?

What is the best way to find right service for dependency injection? Googling is not efficient yet. For example I spent quite long time figuring out that if I need current request passed to ...
1
vote
0answers
16 views

How to show different options for a dependent field

I've 2 options for a dependent field, I would like to show only one option from the dependent field, when the dependees field has a particular value. Dependees is a radio button. If the value is '...
6
votes
1answer
1k views

Why isn't my service found?

I'm trying to load a service into my Drupal module, but even after checking twice whether I did something wrong, I can't seem to find any error in my code as I did everything as the documentation said ...
7
votes
3answers
6k views

How to inject the database connection into a custom service?

I have a custom module that pulls data from several different external databases that have been defined as connections in settings.php. The current setup includes a series of controllers that have a ...
1
vote
1answer
4k views

How to convert objects into array using serialization API?

I'm trying to convert entity objects into array but it's not working. Can you please help me spotting the mistake. I need to use `serializer.normalizer.content_entity' service. The code I have written ...
12
votes
1answer
10k views

Correct way to call a service in a controller

I'm new to services (and drupal 8!) and trying to figure out services. I think perhaps I'm just doing the create in an outdated way. What I want do is make my 'hello generator' a service and call it ...
1
vote
1answer
994 views

Dependency Injection while Unit Testing

Normally with Drupal 8 I can use dependency injection like this: <?php class ExampleController extends ControllerBase { private $http_client; public function __construct(HttpClient $...
16
votes
3answers
5k views

"Drupal calls should be avoided in classes, use dependency injection instead"

In my module using below code for getting url alias of given url: $alias = \Drupal::service('path.alias_manager')->getPathByAlias($_POST['url']); But one I run Automated Review(http://pareview.sh/...
0
votes
1answer
616 views

how to load 2 services (or more) through dependency injection

if i have 1 service, i can load my service through dependency injection like: class MyForm extends FormBase { protected $coreService; /** * Class constructor. */ public function ...
0
votes
1answer
361 views

inject translator service in my service

I am confused by this one. I want the translator service injected in my service. The closes thing I've found is this one: Drupal\Core\StringTranslation\TranslationManager; ( https://api.drupal.org/...
6
votes
2answers
2k views

Dependency injection not working custom form

I have a custom module with a form class, where I am trying to inject a custom service to use within it's submit method: namespace Drupal\my_module\Form; use Drupal\Core\Form\FormBase; use Drupal\...
0
votes
2answers
2k views

"Non-existed service" errors after clearing the cache

After clearing the cache in Drupal 8 (via the UI) I got "The website encountered an unexpected error. Please try again later." error. It happened for the first time. I haven't introduced any heavy ...
0
votes
1answer
276 views

Modifying argument of programmatically called render service. (e.g.: theme to use) = Get object from service container with modified argument

I use the D8 core render service for generating markup that is indexed in an external search service. The indexing is happening on hook_entity_update, where I call my indexing service. My custom ...
4
votes
1answer
399 views

How to inject a fake class into container when running WebTestBase tests?

I've got a form class, and I'm injecting my custom functionality class into it via constructor. That class is added to the container via the services.yml file defined in my custom module. I'd like to ...
10
votes
1answer
1k views

Dependency Injection in Custom Field Widget

I am trying to inject the entity query service into a custom field widget. This is the relevant code: /** * Plugin implementation of the 'address_options' widget. * * @FieldWidget( * id = "...
9
votes
1answer
4k views

Dependency injection in a custom class

I have a custom module (my_module); Inside this module I have a custom class (MyClass), where I want to use a Drupal service (file_system). This is my code. src/MyClass.php namespace Drupal\...
0
votes
1answer
39 views

Service is being instantiated during installation with unknown entity type call

I have a module that defines two services. Service A has service B as dependency. Service B has some dependencies, one of which is entity type manager. In the constructor of the service B an entity ...

1
2