We changed our privacy policy. Read more.

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
0answers
56 views

How to use methods from entity class if it doesn't have such service?

I'm writing an EventSubscriber for the config entity of the domain module. My goal is to insert some data in its config on preSave event (it becomes from the patch for D9). Here is my code from ...
0
votes
0answers
51 views

What is the PHP closure function used to initialize a class actually doing? [closed]

I have some Drupal classes that provide wrapper functionality for entities, which are initialized by a closure (anonymous function) following the class declaration as follows: <?php namespace ...
7
votes
2answers
2k views

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

Sometimes I encounter a public static function create() in plugin classes, for the example in the code show in Tutorial on Using Drupal 8 Plugin Derivatives Effectively. public static function create(...
2
votes
1answer
49 views

How to solve this error "Class \Drupal\simple_sitemap\Controller\SimpleSitemapController does not exist"

I got this error which is kind of surprising to me since I don't have it on my local environment. What I did is that I compressed the project directory and uploaded it to my cpanel file manager and ...
0
votes
1answer
365 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/...
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 ...
1
vote
1answer
101 views

Return a Closure from a Factory

I have services that depend on request information. I'm trying to create a Factory Factory which will have the Dependency Injection container returns a factory (an anonymous function) that will create ...
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 ...
0
votes
0answers
43 views

Use Injected CacheBackedInterface to invalidate certain bin

I've got a content type called SERVICE (I write it all capitalized to prevent mixture with drupal service) which admin can introduce different SERVICE using this type, it is expected that these ...
1
vote
1answer
65 views

Calling a Method from another Module's Class [duplicate]

I'm dumb and this is embarrassing, but I'm trying to figure out how I properly use a method from a class in other parts of my code. I need to understand the Drupal way, if not just the php way. I have ...
1
vote
1answer
73 views

New module service is dependency on old module service - The service has a dependency on a non-existent service

I have a new module moduleB. It defines a service that is used in the dependency injection of moduleA. moduleA is already enabled, its changes are just to the services definition. The info.yml has ...
0
votes
0answers
53 views

Discover custom services with drush generate

When I create a module and a custom service with drush generate and then create another service, also with drush generate, the first one is not available to inject. What is missing to make it become ...
1
vote
1answer
39 views

Missing proxy classes when container compiles custom ExtensionPass

Context I try to find a way to integrate Webpack Encore into Drupal. Since we can't use Symfony Bundles in Drupal I want to port the bundle to a custom Drupal module. What I have done so far To ...
3
votes
0answers
544 views

How can I use TwigExtension from an existing symfony bundle?

UPDATED Context I'm using Symfony Webpack Encore to take care of all the JS and CSS postprocessing, transpiling and so on... There are different ways to implement webpack, even a module, Webpack ...
0
votes
0answers
69 views

After installing and configuring simplesamlphp_auth module gives Error on clearing cache

I installed simplesamlphp_auth(8x-3.2) module with drupal (9.1.x). SimpleSAMLphp (1.18.8) is configured correctly. saml authentication works fine, but while clearing cache i am getting the below error....
1
vote
1answer
64 views

How to inherit base class dependencies without redefining theme in child class service arguments?

The current system I have a set of services classes that extends the same base class ex: class NodeHandler extends ContentEntityHandlerBase {} class ParagraphHandler extends ContentEntityHandlerBase {}...
2
votes
1answer
95 views

How to inject an object into a service?

I need to do a couple of things: Inject guzzle's httpclient into class A Inject class A into my service What I've tried so far: services: my_module.field_fetcher: class: Drupal\my_module\...
1
vote
2answers
777 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
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() { $...
1
vote
1answer
265 views

How to check config setting in custom service class with dependency injection?

In a custom Service class, I need to check a config setting. I'm able to do this using \Drupal::config(static::SETTINGS) as documented here: https://www.drupal.org/docs/drupal-apis/configuration-api/...
0
votes
2answers
149 views

Drupal 8 service dependency injection [closed]

I am trying to build a service that will leverage the Pathauto service so my class looks like this. namespace Drupal\some_system; use Drupal\pathauto\PathautoGenerator; use Drupal\pathauto\...
0
votes
1answer
115 views

Replace \Drupal::service with Dependency injection in FieldWidget class

I have created an ajax callback inside a custom Field Widget plugin. In the Ajax callback I have called a custom service using $service = \Drupal::service('service.name') I am using phpmd to clean ...
4
votes
2answers
984 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 ...
2
votes
1answer
2k 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
131 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
2answers
71 views

What good is the create() method? [closed]

Most classes are instantiated using create($container) and __construct(...$services) in conjunction, it seems that the more efficient workflow would be to simply pass the container to the constructor ...
5
votes
1answer
349 views

What is the right way of loading a service?

when trying to use a service in drupal actually there is 3 ways , what is the preferred way to use a service and why : passing dependencies directly through the constructor: public function ...
0
votes
2answers
2k 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\...
2
votes
0answers
55 views

How do I mock a service when using BroswerTestBase? [duplicate]

I am trying to use BrowserTestBase to write a functional test for a Form. The form populates some form elements with data retrieved from an external site. The client for this external site is injected ...
0
votes
2answers
978 views

How can I inject container to a class with constructor having arguments?

Fatal error: Declaration of Drupal\migrate_upgrade\MigrateUpgradeDrushRunner::create(Psr\Container\ContainerInterface $container, $options = Array) must be compatible with Drupal\Core\...
0
votes
1answer
62 views

How to use service container correcly?

I want to use service container inside my class so I have this this: class SomeController extends ControllerBase { protected $entityTypeManager; protected $requestStack; public function ...
2
votes
1answer
1k 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 ...
-2
votes
1answer
276 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
78 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
3answers
591 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 ...
0
votes
1answer
126 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'...
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
0answers
234 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
671 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
1answer
941 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
47 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 ...
1
vote
1answer
19 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."...
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 ...
0
votes
0answers
413 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. (...
1
vote
1answer
63 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
591 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
252 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 ...
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::...
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/...
1
vote
2answers
111 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 ...