How to create a service with a custom module

Description

Collection: Drupal 8 Developer PrepChapter: Working With Symfony Components in Drupal 8, Part 1 - Using and Creating Services

At this point you’ve seen how to use existing services, but how do we create one of our own? Again, services allow other modules to override our entire service code and allows for the creation of a single, shared instance of the class, so there’s a lot to be gained. Luckily, it’s pretty easy and we’ll start by creating a class to contain our service.

Transcript

All right. So, we've seen how services are defined, and we've seen how we can use services in our code. The next logical step is to figure out how to define services ourselves, and really the only missing part that we haven't done yet is creating a class to encapsulate our service.

So, that's what we're going to do in this next step. Let's jump to the resource pack directory, and let's copy over the 14th step which is called blindd8ingservice. php adding our own service.

And we'll go over to our source directory inside of our blind d8 module and paste it in, and we'll rename it blindd8ingservice. php. Now, let's open this up and take a look at what it looks like.

It's really simple. There's not a lot to it. First of all, we have these comments, and it includes this contains and then the name of the full name space and class name.

Now, one of the ways that this is useful is that we can use grep or some other file searching tool to find a full name space just by searching the text. We don't need any fancy IDE in order to be able to search class names and figure out what the full name space for a class is. Next we define what name space we're in.

And, again, whenever we're inside of a module creating code for a module, the name space is going to be Drupal slash and then the name of the module. And then we define our class, and we're going to call it blind d8ing service. We could really call this anything we want.

There's no specific convention here that we need to follow, and then within this we can add however many methods would be useful for this particular service. Now, in thinking of a service as just a class with a set of methods and the service can have multiple methods that you call, those methods are kind of utilities of the service. In this particular service or class we only have one method - one thing we can do here.

Just like when we looked at the UUID service it only had one method to generate the UUID. In this service we only have one method, and the goal of it is to get the tagline. So, this might be a s ...

This collection will not only prepare you to work with and understand Drupal 8 under the hood, it will increase your skills and relevance as a developer. We cover Object Oriented Programming (OOP), how to use Composer, what the Symfony framework is and how to use its various components, plus lots, lots more. For developers really wanting to modernize their skill set, this is a great place to start.

Before watching this series, you will need a web environment with Drupal 8 installed. Follow the videos in the Setting Up a Web Environment With Drupal to set up a web environment. In the series, we use a couple different versions of Drupal 8 and provide instructions for installing each one so you can follow along with the series exactly. You can also use the latest version of Drupal 8, but there will be some slight differences between what you see in the videos and how you would put what you see into practice, but working through those differences can be excellent for learning.

Some of the key points we'll be covering include:

How to build basic module scaffolding in Drupal 8

How Object-Oriented Programming (OOP) works and how to apply what you learn to coding in Drupal 8

How to work with Symfony components included in Drupal 8

How to use Composer to keep track of code dependencies and leverage a huge library of PHP components

Who this collection is for

This collection is mostly for Drupal developers who want to upgrade their techniques to work with Drupal 8. Because many of these techniques are applicable to PHP coding in general, it can also be useful if you simply want to develop your PHP skills and wrap your mind around object-oriented programming.

Prerequisites

You will get the most out of this collection if you are comfortable with procedural coding in PHP. We will make references to how code works in Drupal 7, so it will be helpful if you have some Drupal coding experience in particular.

It looks like there might be an issue playing videos in this browser. We're working on better cross-brower experience, but in the meantime please try the latest Chrome or Firefox browsers. Or, you may be able to watch the video directly without progress tracking or transcript: