Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

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
0
votes
1answer
25 views

Form state cache breaks dependency injection [duplicate]

I have a class that extends FormBase and there are 6 services that are injected into the class via Dependency Injection (DI). My problem is when I submit the form it says to a class property that ...
-2
votes
1answer
55 views

How to call method of controller in .module file with dependency injection

I have class named Book. class Popups { private $entityQuery; private $entityTypeManager; public function __construct(QueryFactory $entityQuery, EntityTypeManagerInterface $entity_type_manager)...
-1
votes
1answer
47 views

What is the logic of create method in ContainerInterface class & its argument when being used and how its related to argument of the construct method?

<?php namespace Drupal\custom_changes\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\...
1
vote
1answer
74 views

How do I instantiate a custom class with dependency injection?

I am relatively fresh to the DI party and am struggling to get my head around how exactly to use Dependency Injection. I understand that I can pass requirements as part of a service, but what about in ...
0
votes
1answer
67 views

how to extend FormBuilder::processForm from multiple API endpoints in one class

I'm trying to consolidate my form processing by entity_type, without rebuilding the forms (just using the default forms provided by Drupal. I have dynamic routes pointing to a _controller::method. I'...
0
votes
0answers
57 views

dependency injection by using $this when not in object context

if i´m using the codesniffer function i´m getting two warnings about: 155 | WARNING | \Drupal calls should be avoided in classes, use dependency injection instead If i´m changing the line from: $...
1
vote
2answers
215 views

How do I inject UserInterface into Access Control?

I am trying to create a custom access control in my custom module in Drupal 8. But I am having issues in achieving it. I cant find a proper way to inject UserInterface class in access method. ...
1
vote
2answers
82 views

Problem in dependency injection in drupal custom module service

I face with a problem in defining service in drupal 8.0 my service is : services: tracking.middleware: class: Drupal\mkt_tracking_v2\Middleware\TrackingMiddleware arguments: ['@kernel','@...
0
votes
1answer
37 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
476 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
84 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
38 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
85 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
97 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
308 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
84 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
484 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
752 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
355 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) ...
7
votes
2answers
994 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
339 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
529 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 ...
3
votes
1answer
226 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
489 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')); } ...
-1
votes
2answers
1k 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
113 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
664 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
81 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
1k 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 ...
4
votes
1answer
1k 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
834 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
251 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
3answers
743 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
1k 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
306 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
84 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 ...
4
votes
1answer
3k 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 ...
4
votes
2answers
4k 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
88 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
432 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
3k 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
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
886 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 ...
14
votes
1answer
3k 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
146 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
128 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
64 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
342 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 ...