Implement Rules Events #194

Closed
wants to merge 14 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+788 −0
Diff settings

Always

Just for now

@@ -29,6 +29,38 @@ rules.component.*:
type: rules_expression.[%parent.expression_id]
label: 'Configuration'
+rules.reaction.*:
+ type: config_entity
+ label: 'Reaction rule'
+ mapping:
+ id:
+ type: string
+ label: 'ID'
+ label:
+ type: label
+ label: 'Label'
+ event:
+ type: string
+ label: 'Event'
+ module:
+ type: string
+ label: 'Module'
+ description:
+ type: text
+ label: 'Administrative description'
+ tag:
+ type: string
+ label: 'Tag'
+ core:
+ type: string
+ label: 'Drupal version'

This comment has been minimized.

@fago

fago Jun 25, 2015

Owner

hm?

This comment has been minimized.

@klausi

klausi Jun 26, 2015

Collaborator

That is basically a copy of View.php from core, which also saves the core compatibility flag. Not sure we need it, but I would keep it for now.

@klausi

klausi Jun 26, 2015

Collaborator

That is basically a copy of View.php from core, which also saves the core compatibility flag. Not sure we need it, but I would keep it for now.

+ expression_id:
+ type: string
+ label: 'Rules expression plugin ID'
+ configuration:
+ type: rules_expression.[%parent.expression_id]
+ label: 'Configuration'
+
rules_expression:
type: mapping
label: 'Rules expression'
@@ -109,6 +141,23 @@ rules_expression.rules_rule:
type: rules_expression.[id]
label: 'Actions'
+rules_expression.rules_reaction_rule:
+ type: rules_expression
+ label: "Reaction Rule"
+ mapping:
+ id:
+ type: string
+ label: 'Plugin ID'
+ event:
+ type: string
+ label: 'Event'
+ conditions:
+ type: rules_expression.[id]
+ label: 'Conditions'
+ actions:
+ type: rules_expression.[id]
+ label: 'Actions'
+
rules.context.definition:
type: mapping
label: 'Context definition'
View
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @file
+ * Hook implementations for the Rules module.
+ */
+
+use Symfony\Component\EventDispatcher\GenericEvent;
+
+/**
+ * Implements hook_user_login().
+ */
+function rules_user_login($account) {
+ $event = new GenericEvent($account, ['account' => $account]);
+ $event_dispatcher = \Drupal::service('event_dispatcher');
+ $event_dispatcher->dispatch('rules_user_login', $event);
+}
View
@@ -0,0 +1,7 @@
+rules_user_login:
+ label: 'User has logged in'
+ category: 'User'
+ context:
+ account:
+ type: 'entity:user'
+ label: 'Logged in user'
View
@@ -5,6 +5,9 @@ services:
plugin.manager.rules_action:
class: Drupal\rules\Core\RulesActionManager
parent: default_plugin_manager
+ plugin.manager.rules_event:
+ class: Drupal\rules\Engine\RulesEventManager
+ arguments: ['@module_handler']
plugin.manager.rules_data_processor:
class: Drupal\rules\Context\DataProcessorManager
arguments: ['@container.namespaces', '@module_handler']
@@ -14,3 +17,8 @@ services:
plugin.manager.rules_component:
class: Drupal\rules\Plugin\RulesComponentManager
arguments: ['@container.namespaces', '@module_handler']
+ rules.event_subscriber:
+ class: Drupal\rules\EventSubscriber\GenericEventSubscriber
+ arguments: ['@entity.manager']
+ tags:
+ - { name: 'event_subscriber' }
View
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\rules\Core\RulesEvent.
+ */
+
+namespace Drupal\rules\Core;
+
+use Drupal\Core\Plugin\ContextAwarePluginBase;
+
+/**
+ * Base class for rules events.
+ */
+class RulesEvent extends ContextAwarePluginBase implements RulesEventInterface {
+
+}
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\rules\Core\RulesEventInterface.
+ */
+
+namespace Drupal\rules\Core;
+
+use Drupal\Core\Plugin\ContextAwarePluginInterface;
+
+/**
+ * Describes Rules events with their context.
+ */
+interface RulesEventInterface extends ContextAwarePluginInterface {
+
+}
@@ -0,0 +1,85 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\rules\Engine\RulesEventManager.
+ */
+
+namespace Drupal\rules\Engine;
+
+use Drupal\Component\Plugin\Exception\PluginException;
+use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drupal\Core\Plugin\DefaultPluginManager;
+use Drupal\Core\Plugin\Discovery\YamlDiscovery;
+use Drupal\Core\Plugin\Factory\ContainerFactory;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+
+/**
+ * Plugin manager for Rules events that can be triggered.
+ *
+ * Rules events are primarily defined in *.rules.events.yml files.
+ *
+ * @see \Drupal\rules\Core\RulesEventInterface
+ */
+class RulesEventManager extends DefaultPluginManager {
+
+ use StringTranslationTrait;
+
+ /**
+ * Provides some default values for the definition of all Rules event plugins.
+ *
+ * @var array
+ */
+ protected $defaults = [
+ 'class' => '\Drupal\rules\Core\RulesEvent',
+ ];
+
+ /**
+ * {@inheritdoc}
+ */
+ public function __construct(ModuleHandlerInterface $module_handler) {
+ $this->alterInfo('rules_event');
+ $this->discovery = new YamlDiscovery('rules.events', $module_handler->getModuleDirectories());
+ $this->factory = new ContainerFactory($this, 'Drupal\rules\Core\RulesEventInterface');
+ $this->moduleHandler = $module_handler;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function processDefinition(&$definition, $plugin_id) {
+ parent::processDefinition($definition, $plugin_id);
+ if (!isset($definition['context'])) {
+ return;
+ }
+ // Convert the flat context arrays into ContextDefinition objects.
+ // @todo This code should be removed and we should pass this off to some
+ // annotation reader code that converts plugin defintion parts into
+ // objects.
+ foreach ($definition['context'] as $context_name => $values) {

This comment has been minimized.

@fago

fago Jun 25, 2015

Owner

There is some definition fromArray() constructuror in Rules' context definition class now.

@fago

fago Jun 25, 2015

Owner

There is some definition fromArray() constructuror in Rules' context definition class now.

This comment has been minimized.

@klausi

klausi Jun 26, 2015

Collaborator

good catch, thanks!

@klausi

klausi Jun 26, 2015

Collaborator

good catch, thanks!

+ $values += array(
+ 'required' => TRUE,
+ 'multiple' => FALSE,
+ 'default_value' => NULL,
+ );
+ foreach (['label', 'description'] as $key) {
+ if (isset($values[$key])) {
+ // @todo Dynamic translations are bad! But how can specify
+ // translatable strings in the plugin definition YAML file?
+ $values[$key] = $this->t($values[$key]);
+ }
+ else {
+ $values[$key] = NULL;
+ }
+ }
+ if (isset($values['class']) && !in_array('Drupal\Core\Plugin\Context\ContextDefinitionInterface', class_implements($values['class']))) {
+ throw new PluginException('ContextDefinition class must implement \Drupal\Core\Plugin\Context\ContextDefinitionInterface.');
+ }
+ $class = isset($values['class']) ? $values['class'] : 'Drupal\Core\Plugin\Context\ContextDefinition';
+ $definition['context'][$context_name] = new $class(
+ $values['type'], $values['label'], $values['required'],
+ $values['multiple'], $values['description'], $values['default_value']);
+ }
+ }
+
+}
Oops, something went wrong.