Voici comment utiliser l'autowire avec les hooks orientés objets de drupal 11
à noter l'attribut
#[Autowire(service: 'mon_module.adhesion_manager')]
qui précise le service à utiliser pour les services custom.
<?php
namespace Drupal\mon_module\Hook;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Queue\QueueDatabaseFactory;
use Drupal\mon_module\Entity\DemandeAdhesion;
use Drupal\mon_module\Service\AdhesionManager;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
/**
* Provides hook implementations for entities.
*/
class EntityHooks implements ContainerInjectionInterface {
use AutowireTrait;
/**
* {@inheritdoc}
*/
public function __construct(
protected QueueDatabaseFactory $queueFactory,
#[Autowire(service: 'mon_module.adhesion_manager')] protected AdhesionManager $adhesionManager) {}
/**
* Handles the insertion of a demande adhesion.
*
* @param \Drupal\mon_module\Entity\DemandeAdhesion $demande
* The demande adhesion entity to be processed.
*/
#[Hook('demande_adhesion_insert')]
public function demandeAdhesionInsert(DemandeAdhesion $demande) {
$this->adhesionManager->sendAdhesionNotification($demande);
}
}
Contenus en rapport
Drupal 11.1 va introduire une nouvelle façon d'implémenter les hooks, de manière orientée objet !
Ajouter un commentaire