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.
By dawehner on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Issue links:
Description:
Any entity controller (form, access, render, storage etc.) can implement the \Drupal\Core\Entity\EntityControllerInterface
to have their services injected.
Without service injection:
class ExampleEntityViewBuilder extends EntityViewBuilder {
public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
parent::buildContent($entities, $displays, $view_mode, $langcode);
$site_name = Drupal::config()->get('system.site')->get('name');
}
}
With proper service injection:
class ExampleEntityViewBuilder extends EntityViewBuilder implements EntityControllerInterface {
/**
* Constructs a new ExampleEntityViewBuilder object.
*/
public function __construct($entity_type, array $entity_info, ConfigFactory $config_factory) {
parent::__construct($entity_type, $entity_info);
$this->config = $config_factory->get('system.site');
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static($entity_type, $entity_info, $container->get('config.factory');
}
public function buildContent(array $entities, array $displays, $view_mode, $langcode = NULL) {
parent::buildContent($entities, $displays, $view_mode, $langcode);
$site_name = $this->config->get('name');
}
}
Impacts:
Module developers