Stack Exchange Network

Stack Exchange network consists of 174 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

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

1
vote
2answers
39 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
77 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
65 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
313 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
359 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/ ...
0
votes
0answers
68 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
174 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
155 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
124 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
339 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
62 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
273 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
50 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
595 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 ...
2
votes
1answer
401 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 ...
0
votes
0answers
197 views

Argument must be an instance of GuzzleHttp\Client, none given error

I am the getting following error: __construct() must be an instance of GuzzleHttp\Client; none given My code is the following code. What might be causing the error? example.service.yaml services:...
2
votes
1answer
414 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 ...
1
vote
1answer
118 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
380 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
728 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
145 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
65 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
1k 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
2k 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
49 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
216 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 ...
6
votes
2answers
1k 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 ...
1
vote
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
643 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 ...
11
votes
1answer
1k views

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

I have a call in a Controller class to: \Drupal::moduleHandler()->alter('mymodule_myfunction', $plugin_items); Pareview.sh is telling me that "\Drupal calls should be avoided in classes, use ...
2
votes
2answers
124 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
92 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
37 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
202 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
12 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
221 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
3k 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
544 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 $...
15
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
192 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
93 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/...
1
vote
2answers
890 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
618 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
119 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 ...
3
votes
1answer
277 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 ...
6
votes
1answer
711 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 = "...
4
votes
1answer
2k 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
37 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 ...