I'm writing an EventSubscriber for the config entity of the domain module. My goal is to insert some data in its config on preSave event (it becomes from the patch for D9).
Here is my code from ConfigEventSubscriber class of my custom module:
namespace Drupal\custom_module\EventSubscriber;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\domain\Entity\Domain;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigEventSubscriber implements EventSubscriberInterface {
public function __construct(Domain $domain) {
$this->domain = $domain;
}
/**
* {@inheritdoc}
*
* @return array
* The event names to listen for, and the methods that should be executed.
*/
public static function getSubscribedEvents() {
return [
ConfigEvents::PRESAVE => 'configPreSave'
];
}
/**
* React to a config object being saved.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* Config crud event.
*/
public function configPreSave(ConfigCrudEvent $event) {
$config = $event->getConfig();
$domain_id = $config->getOriginal('id');
if ($config->getStorage()->listAll('domain.record.')) {
$this->domain->addDependencyTrait('module', 'country_path');
$this->domain->setThirdPartySetting('country_path', 'domain_path', $domain_id);
}
}
}
Here is my services.yml file:
services:
Drupal\custom_module\EventSubscriber\ConfigEventSubscriber:
tags:
- { name: 'event_subscriber' }
arguments: ['@domain.element_manager']
It throws an error that instance of DomainElementManager given, but Domain entity expected and it's logical, as far as I'm including $domain instance of Domain in my construct. But, I need the Domain class to use these methods:
addDependency, setThirdPartySettings.
So, my question is how to use these methods correctly in the context of my task?
Any help is appreciated.