Utiliser les injections de dépendances

Pour éviter la redondance de \drupal::service('monservice') dans nos classes, nous allons utiliser les injections de dépendances

Utiliser l’injection de dépendance dans un contrôleur

  1. use Drupal\centreon_status\Service\CentreonStatus;
  2. use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
  3.  
  4. /**
  5.  * Class ServicesController.
  6.  */
  7. class ServicesController extends ControllerBase {
  8.  
  9.   /**
  10.    * CentreonStatus Service.
  11.    *
  12.    * @var \Drupal\centreon_status\Service\CentreonStatus
  13.    */
  14.   protected $centreonstatus;
  15.  
  16.   /**
  17.    * KillSwitch.
  18.    *
  19.    * @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
  20.    */
  21.   protected $killswitch;
  22.  
  23.   /**
  24.    * HostsController constructor.
  25.    *
  26.    * @param \Drupal\centreon_status\Service\CentreonStatus $centreonStatus
  27.    *   CentreonStatus.
  28.    * @param \Drupal\Core\PageCache\ResponsePolicy\KillSwitch $killswitch
  29.    *   KillSwitch.
  30.    */
  31.   public function __construct(CentreonStatus $centreonStatus, KillSwitch $killswitch) {
  32.     $this->centreonstatus = $centreonStatus;
  33.     $this->killswitch = $killswitch;
  34.   }
  35.  
  36.   /**
  37.    * {@inheritdoc}
  38.    */
  39.   public static function create(ContainerInterface $container) {
  40.     return new static(
  41.       $container->get('centreon_status.centreon_status'),
  42.       $container->get('page_cache_kill_switch')
  43.     );
  44.   }
  45.  
  46.   /**
  47.    * Get Content.
  48.    *
  49.    * @return array
  50.    *   renderable array.
  51.    */
  52.   public function getContent() {
  53.     [...]
  54.     $this->killswitch->trigger();
  55.     [...]
  56.   }

Utiliser l’injection de dépendance dans un plugin block

  1. use Symfony\Component\DependencyInjection\ContainerInterface;
  2. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  3. use Drupal\centreon_status\Service\CentreonStatus;
  4.  
  5. /**
  6.  * Provides a 'centreon_status' block.
  7.  *
  8.  * @Block(
  9.  *  id = "centreon_hosts_status",
  10.  *  admin_label = @Translation("Hosts status"),
  11.  * )
  12.  */
  13. class CentreonHostsBlock extends BlockBase implements ContainerFactoryPluginInterface {
  14.  
  15.   /**
  16.    * CentreonStatus service.
  17.    *
  18.    * @var \Drupal\centreon_status\Service\CentreonStatus
  19.    */
  20.   protected $centreonstatus;
  21.  
  22.   /**
  23.    * CentreonServicesBlock constructor.
  24.    *
  25.    * @param array $configuration
  26.    * @param string $plugin_id
  27.    * @param mixed $plugin_definition
  28.    * @param \Drupal\centreon_status\Service\CentreonStatus $centreonStatus
  29.    *   CentreonStatus.
  30.    */
  31.   public function __construct(array $configuration, $plugin_id, $plugin_definition, CentreonStatus $centreonStatus) {
  32.     parent::__construct($configuration, $plugin_id, $plugin_definition);
  33.     $this->centreonstatus = $centreonStatus;
  34.   }
  35.  
  36.   /**
  37.    * {@inheritdoc}
  38.    */
  39.   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  40.     return new static(
  41.       $configuration,
  42.       $plugin_id,
  43.       $plugin_definition,
  44.       $container->get('centreon_status.centreon_status')
  45.     );
  46.   }
  47.  
  48.   /**
  49.    * {@inheritdoc}
  50.    */
  51.   public function build() {
  52.     [...]
  53.     var_dump($this->centreonstatus->getRealtime('hosts'));
  54.     [...]
  55.   }

 

Drupal

Ajouter un commentaire

Image CAPTCHA
Saisir les caractères affichés dans l'image.