-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathConfigEventsSubscriberWithDI.php
69 lines (60 loc) · 1.74 KB
/
ConfigEventsSubscriberWithDI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Drupal\custom_events\EventSubscriber;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class ConfigEventsSubscriberWithDI.
*
* @package Drupal\custom_events\EventSubscriber
*/
class ConfigEventsSubscriberWithDI implements EventSubscriberInterface {
/**
* Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
public $messenger;
/**
* ConfigEventsSubscriberWithDI constructor.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* Messenger service injected during the static create() method.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*
* @return array
* The event names to listen for, and the methods that should be executed.
*/
public static function getSubscribedEvents() {
return [
ConfigEvents::SAVE => 'configSave',
ConfigEvents::DELETE => 'configDelete',
];
}
/**
* React to a config object being saved.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* Config crud event.
*/
public function configSave(ConfigCrudEvent $event) {
$config = $event->getConfig();
$this->messenger->addStatus(__CLASS__ . ' - Saved config: ' . $config->getName());
}
/**
* React to a config object being deleted.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* Config crud event.
*/
public function configDelete(ConfigCrudEvent $event) {
$config = $event->getConfig();
$this->messenger->addStatus(__CLASS__ . ' - Deleted config: ' . $config->getName());
}
}