Skip to content
Snippets Groups Projects
Commit 28e2bd2d authored by Tim Rohaly's avatar Tim Rohaly Committed by Tim Rohaly
Browse files

Issue #3257884 by TR: Rename events

parent f291cd1e
Branches
Tags
No related merge requests found
Showing
with 113 additions and 24 deletions
......@@ -10,7 +10,7 @@ id: bundle_create_event
label: 'Test bundle create event'
events:
-
event_name: tr_rulez_entity_bundle_create
event_name: tr_rulez.entity_bundle_create
description: 'See https://www.drupal.org/project/tr_rulez/issues/3100511'
tags:
- rules_examples
......
......@@ -10,7 +10,7 @@ id: bundle_delete_event
label: 'Test bundle delete event'
events:
-
event_name: tr_rulez_entity_bundle_delete
event_name: tr_rulez.entity_bundle_delete
description: 'See https://www.drupal.org/project/tr_rulez/issues/3100511'
tags:
- rules_examples
......
......@@ -10,7 +10,7 @@ id: user_has_been_blocked
label: 'User has been blocked'
events:
-
event_name: tr_rulez_user_was_blocked
event_name: tr_rulez.user_was_blocked
description: 'Example Rule which shows a system message when a User''s status is changed to blocked'
tags:
- rules_examples
......
......@@ -10,7 +10,7 @@ id: user_has_been_unblocked
label: 'User has been unblocked'
events:
-
event_name: tr_rulez_user_was_unblocked
event_name: tr_rulez.user_was_unblocked
description: 'Example Rule which shows a system message when a User''s status is changed to unblocked'
tags:
- rules_examples
......
......@@ -11,7 +11,7 @@ use Drupal\Component\EventDispatcher\Event;
*/
class BundleCreatedEvent extends Event {
const EVENT_NAME = 'tr_rulez_entity_bundle_create';
const EVENT_NAME = 'tr_rulez.entity_bundle_create';
/**
* The entity type name. 'node', 'user', etc.
......
......@@ -11,7 +11,7 @@ use Drupal\Component\EventDispatcher\Event;
*/
class BundleDeletedEvent extends Event {
const EVENT_NAME = 'tr_rulez_entity_bundle_delete';
const EVENT_NAME = 'tr_rulez.entity_bundle_delete';
/**
* The entity type name. 'node', 'user', etc.
......
......@@ -12,7 +12,7 @@ use Drupal\Component\EventDispatcher\Event;
*/
class UserWasBlockedEvent extends Event {
const EVENT_NAME = 'tr_rulez_user_was_blocked';
const EVENT_NAME = 'tr_rulez.user_was_blocked';
/**
* The user account.
......
......@@ -12,7 +12,7 @@ use Drupal\Component\EventDispatcher\Event;
*/
class UserWasUnblockedEvent extends Event {
const EVENT_NAME = 'tr_rulez_user_was_unblocked';
const EVENT_NAME = 'tr_rulez.user_was_unblocked';
/**
* The user account.
......
......@@ -63,7 +63,7 @@ class EventIntegrationTest extends RulesKernelTestBase {
$config_entity = $this->storage->create([
'id' => 'test_rule',
'events' => [['event_name' => 'tr_rulez_user_was_blocked']],
'events' => [['event_name' => 'tr_rulez.user_was_blocked']],
'expression' => $rule->getConfiguration(),
]);
$config_entity->save();
......@@ -95,7 +95,7 @@ class EventIntegrationTest extends RulesKernelTestBase {
$config_entity = $this->storage->create([
'id' => 'test_rule',
'events' => [['event_name' => 'tr_rulez_user_was_unblocked']],
'events' => [['event_name' => 'tr_rulez.user_was_unblocked']],
'expression' => $rule->getConfiguration(),
]);
$config_entity->save();
......
......@@ -3,7 +3,7 @@
namespace Drupal\Tests\tr_rulez\Unit\Integration\Event;
/**
* Checks that the event "tr_rulez_entity_bundle_create" is correctly defined.
* Checks that the event "tr_rulez.entity_bundle_create" is correctly defined.
*
* @coversDefaultClass \Drupal\tr_rulez\Event\BundleCreatedEvent
* @group tr_rulez
......@@ -16,10 +16,10 @@ class BundleCreatedTest extends EventTestBase {
* Tests the event metadata.
*/
public function testBundleCreatedEvent() {
$plugin_definition = $this->eventManager->getDefinition('tr_rulez_entity_bundle_create');
$plugin_definition = $this->eventManager->getDefinition('tr_rulez.entity_bundle_create');
$this->assertSame('After creating a new entity bundle', (string) $plugin_definition['label']);
$event = $this->eventManager->createInstance('tr_rulez_entity_bundle_create');
$event = $this->eventManager->createInstance('tr_rulez.entity_bundle_create');
$type_context_definition = $event->getContextDefinition('entity_type');
$this->assertSame('string', $type_context_definition->getDataType());
$this->assertSame('Entity type', $type_context_definition->getLabel());
......
......@@ -3,7 +3,7 @@
namespace Drupal\Tests\tr_rulez\Unit\Integration\Event;
/**
* Checks that the event "tr_rulez_entity_bundle_delete" is correctly defined.
* Checks that the event "tr_rulez.entity_bundle_delete" is correctly defined.
*
* @coversDefaultClass \Drupal\tr_rulez\Event\BundleDeletedEvent
* @group tr_rulez
......@@ -16,10 +16,10 @@ class BundleDeletedTest extends EventTestBase {
* Tests the event metadata.
*/
public function testBundleDeletedEvent() {
$plugin_definition = $this->eventManager->getDefinition('tr_rulez_entity_bundle_delete');
$plugin_definition = $this->eventManager->getDefinition('tr_rulez.entity_bundle_delete');
$this->assertSame('After deleting an entity bundle', (string) $plugin_definition['label']);
$event = $this->eventManager->createInstance('tr_rulez_entity_bundle_delete');
$event = $this->eventManager->createInstance('tr_rulez.entity_bundle_delete');
$type_context_definition = $event->getContextDefinition('entity_type');
$this->assertSame('string', $type_context_definition->getDataType());
$this->assertSame('Entity type', $type_context_definition->getLabel());
......
......@@ -3,7 +3,7 @@
namespace Drupal\Tests\tr_rulez\Unit\Integration\Event;
/**
* Checks that the event "tr_rulez_user_was_blocked" is correctly defined.
* Checks that the event "tr_rulez.user_was_blocked" is correctly defined.
*
* @coversDefaultClass \Drupal\tr_rulez\Event\UserWasBlockedEvent
* @group tr_rulez
......@@ -16,7 +16,7 @@ class UserWasBlockedTest extends EventTestBase {
* Tests the event metadata.
*/
public function testUserWasBlockedEvent() {
$event = $this->eventManager->createInstance('tr_rulez_user_was_blocked');
$event = $this->eventManager->createInstance('tr_rulez.user_was_blocked');
$user_context_definition = $event->getContextDefinition('account');
$this->assertSame('entity:user', $user_context_definition->getDataType());
$this->assertSame('Updated user', $user_context_definition->getLabel());
......
......@@ -3,7 +3,7 @@
namespace Drupal\Tests\tr_rulez\Unit\Integration\Event;
/**
* Checks that the event "tr_rulez_user_was_unblocked" is correctly defined.
* Checks that the event "tr_rulez.user_was_unblocked" is correctly defined.
*
* @coversDefaultClass \Drupal\tr_rulez\Event\UserWasUnblockedEvent
* @group tr_rulez
......@@ -16,7 +16,7 @@ class UserWasUnblockedTest extends EventTestBase {
* Tests the event metadata.
*/
public function testUserWasUnblockedEvent() {
$event = $this->eventManager->createInstance('tr_rulez_user_was_unblocked');
$event = $this->eventManager->createInstance('tr_rulez.user_was_unblocked');
$user_context_definition = $event->getContextDefinition('account');
$this->assertSame('entity:user', $user_context_definition->getDataType());
$this->assertSame('Updated user', $user_context_definition->getLabel());
......
<?php
/**
* @file
* Install, update, and uninstall functions for the tr_rulez module.
*/
/**
* Updates the name of the Rules Events provided by tr_rulez.
*
* @see https://www.drupal.org/project/tr_rulez/issues/3257884
*
* This hook updates the names of some events defined by the tr_rulez module.
* All Rules configurations using these events on your site will also be
* updated, but note that if you have exported configurations stored on
* disk as YAML files this update cannot and will not alter those exported
* configurations.
*
* Only configurations that have been imported and exist in your site
* configuration will be affected, and only those configurations that use
* these tr_rulez events.
*
* This is a reversible change. In the highly unlikely case that this update
* causes a problems, the changes may be reverted by uncommenting one line in
* tr_rulez_update_8101() then re-running this update. See the source code in
* tr_rulez.install for details.
*/
function tr_rulez_update_8101() {
// The Events to rename are stored in this array as:
// 'old_event_name' => 'new_event_name'
$renamed_events = [
'tr_rulez_user_was_blocked' => 'tr_rulez.user_was_blocked',
'tr_rulez_user_was_unblocked' => 'tr_rulez.user_was_unblocked',
'tr_rulez_entity_bundle_create' => 'tr_rulez.entity_bundle_create',
'tr_rulez_entity_bundle_delete' => 'tr_rulez.entity_bundle_delete',
];
// This change may be reverted if necessary by uncommenting the following
// line, then re-running this update function.
// $renamed_events = array_flip($renamed_events);
// First, update the rules.registered_events variable in the state service,
// where we store the names of events that trigger currently-enabled
// reaction rules. This will ensure that rules using the old event names
// are never triggered.
$registered_events = \Drupal::state()->get('rules.registered_events');
$new_events = $registered_events;
// Loop over the few registered events. Look up each in the array of renamed
// events. If there's a match, replace the old name with the new name.
foreach ($registered_events as $registered_event) {
$root_name = strstr($registered_event, ":", TRUE) ?: $registered_event;
if (isset($renamed_events[$root_name])) {
unset($new_events[$registered_event]);
$new_event = str_replace($root_name, $renamed_events[$root_name], $registered_event);
$new_events[$new_event] = $new_event;
}
}
// Resave the registered events whether or not there were changes.
\Drupal::state()->set('rules.registered_events', $new_events);
// Now we have to update all stored reaction rules configurations that use
// the renamed events.
$rules = \Drupal::configFactory()->listAll('rules.reaction');
// Loop over configuration entities.
foreach ($rules as $config_id) {
$rule = \Drupal::configFactory()->getEditable($config_id);
// The 'events' config value is an array of events with numerical indices.
// Each array element contains an array with key 'event_name' and value
// holding the name of the event.
$event_array = $rule->get('events');
$new_events = $event_array;
foreach ($event_array as $index => $event) {
$root_name = strstr($event['event_name'], ":", TRUE) ?: $event['event_name'];
if (isset($renamed_events[$root_name])) {
unset($new_events[$index]);
$event['event_name'] = str_replace($root_name, $renamed_events[$root_name], $event['event_name']);
$new_events[] = $event;
}
}
// Replace the config value with the new array. Use array_values()
// to re-index the numerical keys sequentially, starting at 0.
$rule->set('events', array_values($new_events));
// Save the configuration.
$rule->save();
}
}
# User events.
tr_rulez_user_was_blocked:
tr_rulez.user_was_blocked:
label: 'After a user was blocked'
category: 'User'
context_definitions:
account:
type: 'entity:user'
label: 'Updated user'
tr_rulez_user_was_unblocked:
tr_rulez.user_was_unblocked:
label: 'After a user was unblocked'
category: 'User'
context_definitions:
......@@ -15,7 +15,7 @@ tr_rulez_user_was_unblocked:
label: 'Updated user'
# Bundle events.
tr_rulez_entity_bundle_create:
tr_rulez.entity_bundle_create:
label: 'After creating a new entity bundle'
category: 'Entity'
context_definitions:
......@@ -25,7 +25,7 @@ tr_rulez_entity_bundle_create:
bundle_name:
type: 'string'
label: 'Bundle name'
tr_rulez_entity_bundle_delete:
tr_rulez.entity_bundle_delete:
label: 'After deleting an entity bundle'
category: 'Entity'
context_definitions:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment