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:

  1. Shows how to create a service.
  2. Shows how to take advantage of inversion of control and container injection.
  3. 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

Issue fork examples-2587389

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mile23 created an issue. See original summary.

Mile23’s picture

Status: Active » Needs review
FileSize
11.28 KB

Here's a starting point. Clearly it's copied and modified from page_example.

Still needs: Tests, better documentation.

Mile23’s picture

Some 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. :-)

Status: Needs review » Needs work

The last submitted patch, 3: 2587389_3.patch, failed testing.

Mile23’s picture

Needs: 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 and service_example. One shows how and when to use \Drupal vs. injection, the other talks about defining services and how to replace existing ones.

joshi.rohit100’s picture

And there should be an example of factories as well.

jungle’s picture

Version: 8.x-1.x-dev » 4.0.x-dev

Prashant.c’s picture

Error in file src/tests/src/Functional/ServiceExampleTest.php

Drupal\Tests\BrowserTestBase::$defaultTheme is required. See         
         https://www.drupal.org/node/3083055, which includes recommendations  
         on which theme to use.