Skip to content

Instantly share code, notes, and snippets.

@levmyshkin
Created November 19, 2021 14:27
Show Gist options
  • Save levmyshkin/1656f00b1792e0d532d0e582bd9b4383 to your computer and use it in GitHub Desktop.
Save levmyshkin/1656f00b1792e0d532d0e582bd9b4383 to your computer and use it in GitHub Desktop.
Drupal Dependencies injection in a custom class/service
services:
book.manager:
class: Drupal\book\BookManager
arguments: ['@entity.manager', '@string_translation', '@config.factory', '@book.outline_storage', '@renderer']
<?php
namespace Drupal\book;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
...
/**
* Defines a book manager.
*/
class BookManager implements BookManagerInterface {
/**
* Entity manager Service Object.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Config Factory Service Object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Books Array.
*
* @var array
*/
protected $books;
/**
* Book outline storage.
*
* @var \Drupal\book\BookOutlineStorageInterface
*/
protected $bookOutlineStorage;
/**
* Stores flattened book trees.
*
* @var array
*/
protected $bookTreeFlattened;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Constructs a BookManager object.
*/
public function __construct(EntityManagerInterface $entity_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, BookOutlineStorageInterface $book_outline_storage, RendererInterface $renderer) {
$this->entityManager = $entity_manager;
$this->stringTranslation = $translation;
$this->configFactory = $config_factory;
$this->bookOutlineStorage = $book_outline_storage;
$this->renderer = $renderer;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment