The "set and forget" feature has been deprecated because the improved access checker in #3505948: Add a new HostEntity method isOpenForRegistration now enables and disables register routes automatically based on registration settings open and close dates. Site administrators can check the Enable box within the per-host-entity registration settings, add the open and close dates to those settings, and the access checker does the rest, hiding or displaying the Register tab, for example, at the appropriate times. See this related change record for more information about the access checker.
The "set and forget" Cron task will continue to run for existing installs that have that option enabled. Site administrators can disable the option via the global settings page at /admin/structure/registration-settings. This should be done in a test environment first to ensure there are no hidden dependencies on the "set and forget" feature for a particular site.
Some existing installs may have custom modules that depend on the updates that the "set and forget" feature was making to registration settings entities. An example of this is shown below. Sites with a custom module similar to this can follow the example and transition to a new event subscriber model that does not require these updates. If you do not have a custom module with this type of dependency, you can stop reading here.
New Events
The registration:3.4.0 release adds new events REGISTRATION_SETTINGS_OPEN and REGISTRATION_SETTINGS_CLOSE. These events are triggered by scheduling event dispatch via the registration_scheduled_action submodule. For example, to trigger the "open" event, follow these steps.
- Enable the
registration_scheduled_actionsubmodule - Browse to the registration schedule at
/admin/structure/registration/schedule - Click the "Add scheduled action" button
- Enter a label, then choose "0 minutes after" and "Dispatch registration open event" and Save
Repeat for the "close" event if needed. Then ensure you have a properly configured Cron task that runs at least once an hour. The Cron job will trigger the appropriate actions for newly opened and newly closed events.
Note: the events are triggered based on the dates, regardless of the value of the "status" field.
Once the actions are triggered, you can use an Event subscriber to process them, using example code below.
Before
Example:
The "set and forget" featured is enabled, toggling the status field on newly opened and closed settings entities.
Cron is running once per hour.
A hook is reacting to newly opened events.
function mymodule_registration_settings_update(EntityInterface $settings_entity) {
if ($settings_entity->original) {
$status = (bool) $settings_entity->getSetting('status');
$original_status = (bool) $settings_entity->original->getSetting('status');
if (!$original_status && $status) {
// A newly opened event.
$host_entity = $settings_entity->getHostEntity();
$node = $host_entity->getEntity();
// Take some action on the host node.
}
}
}
After
Example:
The "set and forget" feature is not enabled.
Event dispatch has been scheduled using the registration_scheduled_action module.
Cron is running once per hour.
An event subscriber is reacting to newly opened events.
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
RegistrationEvents::REGISTRATION_SETTINGS_OPEN => 'onOpen',
];
}
/**
* Processes open.
*
* @param \Drupal\registration\Event\RegistrationSettingsEvent $event
* The registration settings event.
*/
public function onOpen(RegistrationSettingsEvent $event) {
$settings = $event->getSettings();
$host_entity = $settings->getHostEntity();
if ($host_entity->isOpenForRegistration()) {
// A newly opened event.
$node = $host_entity->getEntity();
// Take some action on the host node.
}
}