As always the full example code you may find on GitHub. You may ask "What else could be injected?".Well here is a list of some of the services that you could inject. Underneath you will see the output that we are getting from our newly declared method:Īnd that's it. Then we are simply using the method getDisplayName of the service to show the current logged in user, who is supposed to be the owner of the cowy. The difference is that we declared the _construct function, where we are getting the injected service and we are saving it into our private variable, called currentUser. Return $this->currentUser->getDisplayName() Here is the modified service declaration, essentially we are just passing the service machine name as an argument, starting with services:Īrguments: the change to our class that will enable us to catch the injected service object: currentUser = $currentUser Let's modify a bit our code in order to inject the current_user service into our own service and use it to say that we (the current logged in user) is the owner of the cowy. You could just stop here and be completely happy with the things you've learned, but if you want to learn something about these fairytale creatures that are coming from the fog that are using Dependency Injection - keep reading. In order to instantiate our service, we only have to use in the general case the following code and we can directly make it do something: $our_service = \Drupal::service('drupalup_w') Īnd here is the output and how our cattle looks like: We simply go under /devel/php and we make sure that before that we have activated our newly created module. Let's go ahead and test it with the help of the devel module and the "Execute PHP Code" page that it has. In order to wrap it up - we just need to create a module around it, simply create an info file and it should work ( here is the final version of our module). It is a good idea to have them namespaced with your module name and the class declaration simply follows the namespace, declared in your service file the name of the class. So in our case, we created a file called drupalup_ and the declaration could be something like this: services:Ĭlass: Drupal\drupalup_service\CowServiceĪnd that's basically the simplest service on earth. We are doing it with the help of the *.services.yml located in your module. The next thing we want to do is to declare our class as a service. Our silly class has it's namespace defined and the other part is a quite normal PHP class. Our class PHP file is called CowService.php and contains this: sounds Let's create a dummy class and give it some general methods. Basically, it is a normal PHP class, that is supposed in the general case to be located in your module under the /src folder. Let's see first how we may declare a service class inside of our Drupal 8 module. In this article, we are going to see how we could create basically the simplest Drupal 8 service and then we are going to inject some stuff to it in order to see also how Dependency injection works.Īs always you will get the full code example, wrapped in its own module in order to be easier for you to go for it, test it, change it or just compare it with the version that you are going to build.
0 Comments
Leave a Reply. |