Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Drupal 8 introduces the concept of a service discovery container and services.
We need an example of how to create a service.
Proposed resolution
Create a service_example module.
Some requirements:
- Shows how to create a service.
- Shows how to take advantage of inversion of control and container injection.
- Shows how and when to use \Drupal.
Also checklist stuff: #2209627: [meta] Module Checklist for Examples
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#3 | interdiff.txt | 15.5 KB | Mile23 |
#3 | 2587389_3.patch | 10.39 KB | Mile23 |
| |||
#2 | 2587389_2.patch | 11.28 KB | Mile23 |
|
Issue fork examples-2587389
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
Mile23Here's a starting point. Clearly it's copied and modified from page_example.
Still needs: Tests, better documentation.
Comment #3
Mile23Some work for coding standards and stuff.
Also added a service parameter to demonstrate dependencies.
This patch should fail since the test is set to fail. Since there aren't really any tests. :-)
Comment #5
Mile23Needs: Demo of how to replace an existing service. There are instructions here: https://api.drupal.org/api/drupal/core%21core.api.php/group/container/8
We might want to split this up into two two modules:
container_example
andservice_example
. One shows how and when to use \Drupal vs. injection, the other talks about defining services and how to replace existing ones.Comment #6
joshi.rohit100And there should be an example of factories as well.
Comment #7
jungleComment #9
Prashant.cError in file
src/tests/src/Functional/ServiceExampleTest.php