RulesEventManager.php in Rules 8.3
File
src/Core/RulesEventManager.php
View source
<?php
namespace Drupal\rules\Core;
use Drupal\Component\Plugin\CategorizingPluginManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Core\Plugin\Factory\ContainerFactory;
use Drupal\rules\Context\ContextDefinition;
class RulesEventManager extends DefaultPluginManager implements CategorizingPluginManagerInterface {
use CategorizingPluginManagerTrait;
protected $defaults = [
'class' => RulesDefaultEventHandler::class,
];
protected $entityBundleInfo;
public function __construct(ModuleHandlerInterface $module_handler, EntityTypeBundleInfoInterface $entity_bundle_info) {
$this
->alterInfo('rules_event');
$this->discovery = new ContainerDerivativeDiscoveryDecorator(new YamlDiscovery('rules.events', $module_handler
->getModuleDirectories()));
$this->factory = new ContainerFactory($this, RulesEventHandlerInterface::class);
$this->moduleHandler = $module_handler;
$this->entityBundleInfo = $entity_bundle_info;
}
public function createInstance($plugin_id, array $configuration = []) {
$plugin_id = $this
->getEventBaseName($plugin_id);
return parent::createInstance($plugin_id, $configuration);
}
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
$base_plugin_id = $this
->getEventBaseName($plugin_id);
$definition = parent::getDefinition($base_plugin_id, $exception_on_invalid);
if ($base_plugin_id != $plugin_id) {
$parts = explode('--', $plugin_id, 2);
$entity_type_id = explode(':', $parts[0], 2);
$bundles = $this->entityBundleInfo
->getBundleInfo($entity_type_id[1]);
$definition['label'] = $definition['label'] . " of type " . $bundles[$parts[1]]['label'];
}
return $definition;
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if (!isset($definition['context_definitions'])) {
$definition['context_definitions'] = [];
}
foreach ($definition['context_definitions'] as $context_name => $values) {
$definition['context_definitions'][$context_name] = ContextDefinition::createFromArray($values);
}
}
public function getEventBaseName($event_name) {
if (strpos($event_name, '--') !== FALSE) {
$parts = explode('--', $event_name, 2);
return $parts[0];
}
return $event_name;
}
}