Initial commit
This commit is contained in:
commit
899c896455
4
README.md
Normal file
4
README.md
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# Autowire Example
|
||||||
|
|
||||||
|
An example module to demonstrate autowiring service classes in Drupal 8, as
|
||||||
|
well as how to define Controllers as Services.
|
5
autowire_example.info.yml
Normal file
5
autowire_example.info.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
name: 'Autowire Example'
|
||||||
|
type: module
|
||||||
|
description: 'An example module to demonstrate autowiring in Drupal 8.'
|
||||||
|
core: 8.x
|
||||||
|
package: 'Example modules'
|
6
autowire_example.routing.yml
Normal file
6
autowire_example.routing.yml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
autowire_example.user_count:
|
||||||
|
path: '/user-count'
|
||||||
|
defaults:
|
||||||
|
_controller: 'Drupal\autowire_example\Controller\ExampleController::__invoke'
|
||||||
|
requirements:
|
||||||
|
_access: 'TRUE'
|
6
autowire_example.services.yml
Normal file
6
autowire_example.services.yml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
services:
|
||||||
|
Drupal\autowire_example\Service\UserCounter:
|
||||||
|
autowire: true
|
||||||
|
|
||||||
|
Drupal\autowire_example\Controller\ExampleController:
|
||||||
|
autowire: true
|
48
src/Controller/ExampleController.php
Normal file
48
src/Controller/ExampleController.php
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\autowire_example\Controller;
|
||||||
|
|
||||||
|
use Drupal\autowire_example\Service\UserCounter;
|
||||||
|
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An example controller.
|
||||||
|
*/
|
||||||
|
class ExampleController {
|
||||||
|
|
||||||
|
use StringTranslationTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user counter service.
|
||||||
|
*
|
||||||
|
* @var \Drupal\autowire_example\Service\UserCounter
|
||||||
|
*/
|
||||||
|
private $userCounter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ExampleController constructor.
|
||||||
|
*
|
||||||
|
* @param \Drupal\autowire_example\Service\UserCounter $user_counter
|
||||||
|
* The user counter service.
|
||||||
|
*/
|
||||||
|
public function __construct(UserCounter $user_counter) {
|
||||||
|
$this->userCounter = $user_counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the username of the current user.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* A render array.
|
||||||
|
*/
|
||||||
|
public function __invoke() {
|
||||||
|
return [
|
||||||
|
'#markup' => $this->formatPlural(
|
||||||
|
number_format($this->userCounter->getActiveUserCount()),
|
||||||
|
'This site has 1 active user.',
|
||||||
|
'This site has @count active users.'
|
||||||
|
)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
46
src/Service/UserCounter.php
Normal file
46
src/Service/UserCounter.php
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\autowire_example\Service;
|
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A service to count users.
|
||||||
|
*/
|
||||||
|
class UserCounter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The user storage instance.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||||
|
*/
|
||||||
|
private $userStorage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UserCounter constructor.
|
||||||
|
*
|
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
||||||
|
* The entity type manager.
|
||||||
|
*
|
||||||
|
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
|
||||||
|
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
|
||||||
|
*/
|
||||||
|
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
|
||||||
|
$this->userStorage = $entity_type_manager->getStorage('user');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Count the active users.
|
||||||
|
*
|
||||||
|
* @return integer
|
||||||
|
* The number of active users.
|
||||||
|
*/
|
||||||
|
public function getActiveUserCount() {
|
||||||
|
return (int) $this->userStorage
|
||||||
|
->getQuery()
|
||||||
|
->condition('status', 1)
|
||||||
|
->count()
|
||||||
|
->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue