Objectives & prerequisites
- Create a new my_services_demo module that uses several services.
- Combine what you learned about routing, the current_user service, and the translation service.
- Basic understanding of the concept of services.
- Knowing you can access services through your object (if it contains services) or through the service container.
- Basic use of two services in action: one to retrieve the current user, and one to make text translatable.
New module: my_services_demo
Let's create a new module, my_services_demo, so you can play around with some services and get more experience building modules. This module will start out being almost identical to your earlier module, my_route_demo.
In each of the following units of this chapter you will add more functionality to this new module.
The following task takes you through the process of creating this new module step by step. As you progress through the next units and become more familiar with creating modules and using services, the instructions will become less detailed.
Activity: create a new module
- Create a new module in /modules/custom/ called my_services_demo.
- Add an info file to your module
- Ensure its YAML syntax is valid. Refer to your my_route_demo if you're unsure of how to construct the info file.
- Ensure the info file has the correct file extension (.info.yml).
- Ensure its YAML syntax is valid. Refer to your my_route_demo if you're unsure of how to construct the info file.
- When everything looks in order, clear your cache.
- Check if your module is listed on the modules page under Manage > Extend.
- If your module is not listed, verify the previous steps carefully.
- If your module is listed, enable it.
- Hint: so far this is exactly what you did earlier with your my_route_demo module.
Activity: add a routing file
- In your new module, create a routing file in the right place, with the right name. If you're unsure how to proceed, refer to Creating a basic route or my_route_demo.
- Add a route definition to your routing file. As a starting point, copy/paste the first route you added to
my_route_demo.routing.yml, but make the following changes:- Set the route's name to
my_services_demo.current_user - Set path: to
'/current_user' - Set _controller: to
'\Drupal\my_services_demo\Controller\UserController::currentUserDemo' - Set _title: to
'Current User'
- Set the route's name to
Activity: add a controller file
- Create a
srcdirectory. Inside it, create aControllerdirectory - Add a controller class file named
UserController.phpto your module; place it inside your/src/Controllerdirectory. - Copy/paste the contents from
HelloController.phpintoUserController.php. - Change the namespace so it contains
my_services_demoinstead ofmy_route_demo. - Change the name of the class from 'HelloController' to
UserController. - Remove the three methods (
hello,helloName,addition) from the class.
After you've done all thism your class should look like this:
// [...]
class UserController extends ControllerBase {
}→ don't clear your cache yet, you're not finished.
In your routing file, you've previously set your route to point to UserController::currentUserDemo.
Here's that line from your routing file again:
_controller: '\Drupal\my_services_demo\Controller\UserController::currentUserDemo'In the next task you will add this currentUserDemo method to your UserController class.
Activity: add a controller method
Add a method named currentUserDemo to your controller.
After you've done this, your class definition should look like this:
// [...]
class UserController extends ControllerBase {
public function currentUserDemo() {
}
}→ Now it's up to you to complete the controller method.
The purpose of this method is to print "Hello, Jane" on screen, assuming the current user's username is "Jane".
- You've already seen how to use the
currentUser()service to retrieve the current user's account information. You can access this service inside your class via$this, or you can use the service container (\Drupal). Either choice is fine. - The class that is returned by
currentUser()provides thegetDisplayName()method. You can use it to retrieve the current user's name. - Be sure to clear your cache after updating your routing file or controller file.
→ Now complete the controller method, clear your cache, and check if you see your username appear when you browse to /current_user.
If you're not used to writing Object Oriented PHP code, there will be a lot of trial and error until you get this right. This is normal. Don't hesitate to refer back to the refresher on Object Oriented Programming.
Even though it can get frustrating sometimes, don't give up! Everyone struggles with Drupal's steep learning curve.
If you're stuck, feel free to inspect the solution source code, but don't just copy/paste from there. Rather, compare your code with the solution and try to figure out where you made a mistake.
Summary
- You can use the
current_userservice to retrieve information about the current user. - You've learned that the
current_userservice provides a method calledgetDisplayName()which returns a name as a string of text.