Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [dependency-injection]

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

0
votes
1answer
31 views

How Webprofiler EntityManagerWrapper overrides EntityTypeManager?

In trying to understand the new paradigm of Dependency Injection, I attempted to install Webprofiler into my D8.6.10 project but it seemed it causes a conflict with my already installed ...
2
votes
1answer
34 views

How to call User::load with dependency injection

I have code that worked, but I wanted to clean it up and use dependency injection. I'm working in a BlockBase that implements ContainerFactoryPluginInterface. class DefaultBlock extends BlockBase ...
1
vote
1answer
18 views

Front Blank Webpage

Hi I am working on moving my website from local to development, but get this error: Uncaught PHP Exception InvalidArgumentException: "Class "Drupal\entity\QueryAccess\ViewsQueryAlter" does not exist."...
0
votes
0answers
38 views

Using a class to call a call a custom drush service

I have created a drush service and I can use it on the command line, but I am trying to use dependency injection to use it from another class. However, it is saying that the service cannot be found. (...
0
votes
1answer
31 views

Get Plugin Definition methods using dependency injection

It is my first time defining a custom Plugin Type. I think that this might be a comming thing in Drupal but I was not able to find any documentation anywhere. I want to get Plugin definition methods ...
0
votes
1answer
44 views

How to properly call dependency classes (Class not found)

I'm creating a custom module that uses PHPMailer, which has been installed in the module's vendor folder via composer. This is the error I get: Error: Class 'PHPMailer' not found in Drupal\multisend\...
0
votes
1answer
43 views

Instantiate Controller with Interface not concrete class [closed]

As background, I am rebuilding some old code and want to wrap all the curl based requests in an interface (HttpRequestInterface) so I can swap that out later without having to write all the api calls ...
1
vote
1answer
121 views

Dependency Injection if service exists

Is there any way to use dependency injection in a conditional way, i.e. only if a service exists similar to \Drupal::hasService(). I have a created a service in module A and in module B I have an ...
1
vote
2answers
60 views

Issue with the cognito module

I am running into the below issue : The website encountered an unexpected error. Please try again later.InvalidArgumentException: Class "\Drupal\cognito\Form\Email\UserLoginForm" does ...
0
votes
2answers
234 views

How to use dependency injection to apply a class static method?

I'm building a custom form (extending ConfigFormBase) and using static method File::load($fid). How to use dependency injection to call this method? Thank you!
1
vote
2answers
241 views

Dependency Injection Syntax

I'm trying to build a simple module (to learn DI) to return the current user ID, and and apparently I'm missing something in the syntax. Here's my code and the error it's producing: use Drupal\Core\...
3
votes
1answer
325 views

Difference between calling a service method in drupal 8

I want to know what difference does it make when i call a service this way : $service = \Drupal::service('myservice.method'); $service->methodNameInService(); or this way (Dependency Injecton) ...
4
votes
2answers
494 views

What is public static function create() in plugins for?

Sometimes I encounter a function public static function create with arguments in plugin classes. For example, here https://www.sitepoint.com/tutorial-on-using-drupal-8-plugin-derivatives-effectively/ ...
1
vote
1answer
180 views

how to inject dependencies into an access controller?

I am altering an existing route and adding a custom access. The custom access is a method that I want to have it in it's own access controller: /** * Alters existing routes. */ public ...
0
votes
1answer
320 views

Creating a simple Drupal 8 custom service - argument should be an instance of a class, recognised as an array

I'm kind of new to Drupal 8 development and object-oriented PHP in general. I've been trying to make a simple custom block module, which includes a service with simple dependency injection. However, I ...
4
votes
1answer
201 views

How can I inject a different service in an existing one, i.e. exchange one already injected service by an own one?

The basic authentication provider service uses the user.auth service: basic_auth.authentication.basic_auth: class: Drupal\basic_auth\Authentication\Provider\BasicAuth arguments: ['@config....
1
vote
1answer
271 views

Why I should use dependency injection in Drupal 8? [closed]

Why is this __construct(MyService $my_service ) { $this->my_service = $my_service; } create (ContainerInterface $container) { return new static( $container->get('my.service')); } ...
0
votes
2answers
642 views

Dependency injection or \Drupal class methods?

Suppose I have an ExampleService class and I need a service (current_user) to process my logic. So I have implemented my service like this. The way 1: DI standard Drupal namespace Drupal\...
1
vote
1answer
85 views

How to instantiate a service inside ServiceProvider?

I need to instantiate a service inside ServiceProvider. At that point the container is not built. Any ideas or useful links? <?php namespace Drupal\my_module; use Drupal\Core\...
1
vote
0answers
455 views

How to understand and fix a Reflection does not exist error?

I am trying to get my head around using EventSubscriber for adding HTTP Headers. In D7 I would have simply used drupal_add_http_header('Content-Type', 'application/javascript'); But I see that this ...
0
votes
1answer
67 views

Avoiding entity-type errors on module installation

I created a logger service that uses a config entity for configuration: https://drupal.org/project/dblog_persistent Unfortunately, it appears that this module is not installable on a new site, ...
4
votes
1answer
795 views

how to unit test an http client service with DI in drupal 8?

I have a service the consumes data from an external source. I would like to unit test the service and I am not sure how to get the test set up. To start with I would like to just test 1 method. Here ...
3
votes
1answer
648 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 ...
2
votes
1answer
540 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 ...
2
votes
1answer
178 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 $...
0
votes
2answers
471 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() { $...
4
votes
1answer
980 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
219 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
73 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 ...
3
votes
1answer
2k 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 ...
2
votes
2answers
3k 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
65 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 ...
0
votes
3answers
298 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 ...
5
votes
2answers
2k views

Dependency injection to load node data

When i checked DrupalPractice standard using phpcs (phpcs --standard=DrupalPractice /path/to/myController ) for a controller file am getting the following warning WARNING | Node::load calls ...
2
votes
1answer
2k 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
725 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('...
4
votes
2answers
1k 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 ...
13
votes
1answer
2k 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
135 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
110 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
42 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
263 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
13 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
391 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 ...
0
votes
1answer
2k 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 ...
7
votes
1answer
4k 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 ...
0
votes
1answer
620 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
2k 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
244 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
130 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/...