Drupal - How to use dependency injection for Plugins/Blocks [\Drupal::formbuilder() and \Drupal::config()]?
For future reference, from the link by @Kevin:
The key to making plugins use dependency injection is to implement the ContainerFactoryPluginInterface. When plugins are created, the code first checks if the plugin implements this interface. If it does, it uses the create() and __construct() pattern, if not, it uses just the __construct() pattern. - From Acquia's Lesson 11.4 - Dependency injection and plugins
The example code below is written by Mart Matsoo from Chromatic. Please check the full article here: https://chromatichq.com/blog/dependency-injection-drupal-8-plugins. As @Kevin already mentioned in the comments it is a great article about Depencency Injection in Plugins.
Dependency Injection example in a block
Note the injection of the 'current_user' service via $container->get('current_user')
in the create()
method. You can pass extra services there.
<?php
namespace Drupal\heytaco\Plugin\Block;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a Hey Taco Results Block
*
* @Block(
* id = "heytaco_block",
* admin_label = @Translation("HeyTaco! Leaderboard"),
* )
*/
class HeyTacoBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* @var $account \Drupal\Core\Session\AccountProxyInterface
*/
protected $account;
/**
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param array $configuration
* @param string $plugin_id
* @param mixed $plugin_definition
*
* @return static
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_user')
);
}
/**
* @param array $configuration
* @param string $plugin_id
* @param mixed $plugin_definition
* @param \Drupal\Core\Session\AccountProxyInterface $account
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $account) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->account = $account;
}
Dependency Injection example in a controller
The same concept applies here, pass extra services via $container->get()
in the create()
method.
<?php
namespace Drupal\heytaco\Controller;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;
/**
* Controller routines for HeyTaco! block.
*/
class HeyTacoController extends ControllerBase {
protected $account;
public function __construct(AccountInterface $account) {
$this->account = $account;
}
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user')
);
}