Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Milkovsky feature/rul 44 #430

Open
wants to merge 39 commits into
base: 8.x-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ddddfd2
Added EventHandlerEntityBundle.
milkovsky Feb 5, 2016
52317fd
Added EventHandlerEntityBundle to events yml.
milkovsky Feb 5, 2016
b60b780
Started writing EventHandlerTest. WIP.
milkovsky Feb 5, 2016
753833d
EventHandlerTest WIP.
milkovsky Feb 5, 2016
46f1fbe
Improved EventHandlerTest WIP.
milkovsky Feb 5, 2016
066e4ec
PR fixes.
milkovsky Feb 10, 2016
0f1a9e5
Dispatch events by base name of a configured event name.
milkovsky Feb 10, 2016
ecce557
Implemented testEntityBundleHandlerExecution.
milkovsky Feb 10, 2016
54c3cd4
PR fixes.
milkovsky Feb 11, 2016
03ecf3a
PR fixes.
milkovsky Feb 11, 2016
7fbae65
PR fixes.
milkovsky Feb 11, 2016
8e4af1b
PR fixes.
milkovsky Feb 11, 2016
b57e33d
Change rules config to have multiple events.
milkovsky Feb 11, 2016
7d0d7e2
Tests for multiple events.
milkovsky Feb 11, 2016
87dca7d
PR fixes.
milkovsky Feb 15, 2016
a58f03a
Inject the Rules event manager.
milkovsky Feb 15, 2016
cb7f762
Code formatting.
milkovsky Feb 16, 2016
6fecbad
Merge branch '8.x-3.x' of github.com:fago/rules into feature/RUL-45
milkovsky Feb 18, 2016
944aa09
After merge fix.
milkovsky Feb 18, 2016
9ead81e
Initialize events [].
milkovsky Feb 18, 2016
7332b45
Merge branch '8.x-3.x' into feature/2658842-event-handler-per-bundle
milkovsky Feb 18, 2016
346ec1d
Merge branch 'feature/2658842-event-handler-per-bundle' into feature/…
milkovsky Feb 18, 2016
92aacdc
Fix test after merge.
milkovsky Feb 18, 2016
b866850
Added ajax reload.
milkovsky Feb 18, 2016
5b1a9c3
Bundle select + handler. WIP.
milkovsky Feb 19, 2016
c14b531
Merge branch '8.x-3.x' into feature/RUL-44
milkovsky Feb 26, 2016
df48c33
After merge fixes.
milkovsky Feb 26, 2016
8e0c714
Merge branch '8.x-3.x' into feature/RUL-45
milkovsky Feb 26, 2016
3378725
Merge branch 'feature/RUL-45' into feature/RUL-44
milkovsky Feb 26, 2016
1b353fb
Fixed event discovery by base name.
milkovsky Feb 26, 2016
d179fcf
Fixed expression set in rules handler test.
milkovsky Feb 26, 2016
0c297b6
Merge branch '8.x-3.x' into feature/RUL-45
milkovsky Feb 26, 2016
5eea48c
Fix testMultipleEvents test.
milkovsky Feb 26, 2016
ab4e106
Merge branch 'feature/RUL-45' into feature/RUL-44
milkovsky Feb 26, 2016
c936dfc
Implemented summary().
milkovsky Feb 26, 2016
a341b28
Refactoring.
milkovsky Feb 26, 2016
fa7d831
Refactoring.
milkovsky Feb 26, 2016
f6266e3
Merge branch 'feature/RUL-44' of git://github.com/milkovsky/rules int…
fago Mar 7, 2016
7fecba2
Worked over bundle configurable event.
fago Mar 7, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Core/RulesDefaultEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function getContextDefinitions() {
$definition = $this->getPluginDefinition();
if ($this instanceof RulesConfigurableEventHandlerInterface) {
$this->refineContextDefinitions();
$definition = $this->getPluginDefinition();
}
return !empty($definition['context']) ? $definition['context'] : [];
}
Expand Down
27 changes: 26 additions & 1 deletion src/Entity/ReactionRuleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function getExpression() {
*/
public function getComponent() {
$component = RulesComponent::create($this->getExpression());
$component->addContextDefinitionsForEvents($this->getEventNames());
$component->addContextDefinitionsForEvents($this->getEventBaseNames());
return $component;
}

Expand Down Expand Up @@ -253,6 +253,31 @@ public function getEventNames() {
return $names;
}

/**
* Gets the base names of all events the rule is reacting on.
*
* For a configured event name like {EVENT_NAME}--{SUFFIX}, the base event
* name {EVENT_NAME} is returned.
*
* @return string[]
* The array of base event names of the rule.
*
* @see \Drupal\rules\Core\RulesConfigurableEventHandlerInterface::getEventNameSuffix()
*/
public function getEventBaseNames() {
$names = [];
foreach ($this->events as $event) {
$event_name = $event['event_name'];
if (strpos($event_name, '--') !== FALSE) {
// Cut off any suffix from a configured event name.
$parts = explode('--', $event_name, 2);
$event_name = $parts[0];
}
$names[] = $event_name;
}
return $names;
}

/**
* {@inheritdoc}
*/
Expand Down
5 changes: 2 additions & 3 deletions src/Entity/ReactionRuleStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
protected function getRegisteredEvents() {
$events = [];
foreach ($this->loadMultiple() as $rules_config) {
foreach ($rules_config->getEventNames() as $event_name) {
$event_name = $this->eventManager->getEventBaseName($event_name);
foreach ($rules_config->getEventBaseNames() as $event_name) {
if (!isset($events[$event_name])) {
$events[$event_name] = $event_name;
}
Expand All @@ -122,7 +121,7 @@ public function save(EntityInterface $entity) {
// otherwise the reaction rule will not fire. However, we can do an
// optimization: if every event was already registered before, we do not
// have to rebuild the container.
foreach ($entity->getEventNames() as $event_name) {
foreach ($entity->getEventBaseNames() as $event_name) {
if (empty($events_before[$event_name])) {
$this->drupalKernel->rebuildContainer();
break;
Expand Down
70 changes: 64 additions & 6 deletions src/EventHandler/ConfigurableEventHandlerEntityBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@
*/
class ConfigurableEventHandlerEntityBundle extends ConfigurableEventHandlerBase {

/**
* The bundles information for the entity.
*
* @var array
*/
protected $bundlesInfo;

/**
* The entity info plugin definition.
*
* @var mixed
*/
protected $entityInfo;

/**
* The entity type.
*
* @var string
*/
protected $entityTypeId;

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeId = $plugin_definition['entity_type_id'];
// @todo: This needs to use dependency injection.
$this->entityInfo = \Drupal::entityTypeManager()->getDefinition($this->entityTypeId);
// @tdo: use EntityTypeBundleInfo service.
$this->bundlesInfo = \Drupal::entityManager()->getBundleInfo($this->entityTypeId);
if (!$this->bundlesInfo) {
throw new \InvalidArgumentException('Unsupported event name passed.');
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -30,42 +66,64 @@ public static function determineQualifiedEvents(Event $event, $event_name, array
* {@inheritdoc}
*/
public function summary() {
// Nothing to do by default.
$bundle = $this->configuration['bundle'];
$bundle_label = isset($this->bundlesInfo[$bundle]['label']) ? $this->bundlesInfo[$bundle]['label'] : $bundle;
$suffix = isset($bundle) ? ' ' . t('of @bundle-key %name', array('@bundle-key' => $this->entityInfo->getBundleLabel(), '%name' => $bundle_label)) : '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never split up translatable messages like this. Use 2 distinct t() calls instead.

Oh, and don't use t(), us $this->t() instead.

return $this->pluginDefinition['label']->render() . $suffix;
}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// Nothing to do by default.
$form['bundle'] = array(
'#type' => 'select',
'#title' => t('Restrict by @bundle', array('@bundle' => $this->entityInfo->getBundleLabel())),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->t()

'#description' => t('If you need to filter for multiple values, either add multiple events or use the "Entity is of bundle" condition instead.'),
'#default_value' => $this->configuration['bundle'],
'#empty_value' => '',
);
foreach ($this->bundlesInfo as $name => $bundle_info) {
$form['bundle']['#options'][$name] = $bundle_info['label'];
}
return $form;
}

/**
* {@inheritdoc}
*/
public function extractConfigurationFormValues(array &$form, FormStateInterface $form_state) {
$this->configuration['bundle'] = $form_state->getValue('bundle');
}

/**
* {@inheritdoc}
*/
public function validate() {
// Nothing to check by default.
// Nothing to validate.
}

/**
* {@inheritdoc}
*/
public function getEventNameSuffix() {
// Nothing to do by default.
return isset($this->configuration['bundle']) ? $this->configuration['bundle'] : FALSE;
}

/**
* {@inheritdoc}
*/
public function refineContextDefinitions() {
// Nothing to refine by default.
if ($bundle = $this->getEventNameSuffix()) {
$this->pluginDefinition['context']['entity']->setBundles([$bundle]);
}
}

/**
* {@inheritdoc}
*/
public function calculateDependencies() {
// Nothing to calculate by default.
// @todo: Implement.
}

}
48 changes: 48 additions & 0 deletions src/Form/ReactionRuleAddForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Drupal\rules\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Core\RulesConfigurableEventHandlerInterface;
use Drupal\rules\Core\RulesEventManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -71,12 +72,37 @@ public function form(array $form, FormStateInterface $form_state) {
'#title' => $this->t('React on event'),
'#options' => $options,
'#required' => TRUE,
'#ajax' => $this->getDefaultAjax(),
'#description' => $this->t('Whenever the event occurs, rule evaluation is triggered.'),
'#executes_submit_callback' => array('::submitForm'),
];

$form['event_configuration'] = array();
if ($values = $form_state->getValue('events')) {
$event_name = $values[0]['event_name'];
if ($handler = $this->getEventHandler($event_name)) {
$form['event_configuration'] = $handler->buildConfigurationForm(array(), $form_state);
}
}

return $form;
}

/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state) {
$entity = parent::buildEntity($form, $form_state);
foreach ($entity->getEventBaseNames() as $event_name) {
if ($handler = $this->getEventHandler($event_name)) {
$handler->extractConfigurationFormValues($form['event_configuration'], $form_state);
$entity->set('configuration', $handler->getConfiguration());
$entity->set('events', [['event_name' => $event_name . '--' . $handler->getConfiguration()['bundle']]]);
}
}
return $entity;
}

/**
* {@inheritdoc}
*/
Expand All @@ -87,4 +113,26 @@ public function save(array $form, FormStateInterface $form_state) {
$form_state->setRedirect('entity.rules_reaction_rule.edit_form', ['rules_reaction_rule' => $this->entity->id()]);
}

/**
* Gets event handler class.
*
* Currently event handler is available only when the event is configurable.
*
* @param $event_name
* The event base name.
* @param array $configuration
* The event configuration.
*
* @return \Drupal\rules\Core\RulesConfigurableEventHandlerInterface|null
* The event handler, null if there is no proper handler.
*/
protected function getEventHandler($event_name, $configuration = []) {
$event_definition = $this->eventManager->getDefinition($event_name);
$handler_class = $event_definition['class'];
if (is_subclass_of($handler_class, RulesConfigurableEventHandlerInterface::class)) {
$handler = new $handler_class($configuration, $this->eventManager->getEventBaseName($event_name), $event_definition);
return $handler;
}
}

}
5 changes: 3 additions & 2 deletions src/Form/ReactionRuleEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ protected function prepareEntity() {
*/
public function form(array $form, FormStateInterface $form_state) {
foreach ($this->entity->getEventNames() as $key => $event_name) {
$event_definition = $this->eventManager->getDefinition($event_name);
$form['event'][$key] = [
$event_base_name = $this->eventManager->getEventBaseName($event_name);
$event_definition = $this->eventManager->getDefinition($event_base_name);
$form['events'][$key] = [
'#type' => 'item',
'#title' => $this->t('Events:'),
'#markup' => $this->t('@label (@name)', [
Expand Down
32 changes: 32 additions & 0 deletions src/Form/RulesComponentFormBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ abstract class RulesComponentFormBase extends EntityForm {
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
// Specify the wrapper div used by #ajax.
$form['#prefix'] = '<div id="rules-form-wrapper">';
$form['#suffix'] = '</div>';

$form['settings'] = [
'#type' => 'details',
'#title' => $this->t('Settings'),
Expand Down Expand Up @@ -78,4 +82,32 @@ public function exists($id) {
return (bool) $this->entityTypeManager->getStorage($type)->load($id);
}

/**
* Get default form #ajax properties.
*
* @param string $effect
* (optional) The jQuery effect to use when placing the new HTML (used with
* 'wrapper'). Valid options are 'none' (default), 'slide', or 'fade'.
*
* @return array
*/
public function getDefaultAjax($effect = 'none') {
return array(
'callback' => '::reloadForm',
'wrapper' => 'rules-form-wrapper',
'effect' => $effect,
'speed' => 'fast',
);
}

/**
* Ajax callback to reload the form.
*
* @return array
* The reloaded form.
*/
public function reloadForm(array $form, FormStateInterface $form_state) {
return $form;
}

}