Web services allow us to provide information to a device over an internet connection, such as lists of contents, entities, users, and more. In Drupal, we use the Services Module to perform these actions. You can explore an example in this blog: [Creando contenido con Rest Services].
In this guide, we will create a custom module to define a service that returns a list of users.
Module Definition: rootstack_services_custom.info
Module Implementation: rootstack_services_custom.module
The module begins by defining permissions using hook_permission()
:
Service Resources: rootstack_services_custom.services.inc
Define the custom service using hook_services_resources()
:
Callback Function: rootstack_services_custom.resources.inc
The callback function retrieves the list of users, with an optional filter by user ID (uid
):
Configuration Steps
- Activate the Module
Navigate to Admin → Modules and activate the custom module. - Assign Permissions
Go to Admin → People → Permissions and assign therootstack get users
permission. - Generate the Service
- Navigate to Admin → Structure → Services.
- From the Resources tab, select the newly created resource.
Creating custom services in Drupal is a powerful and straightforward process. It allows not only database queries but also integration with tools like Solr indexing. Furthermore, it provides fine-grained permission control for each service, enhancing both flexibility and security.
This approach is an essential skill for extending Drupal's functionality to meet unique project needs.