rules.module
Same filename in other branches
Hook implementations for the Rules module.
File
View source
<?php
/**
* @file
* Hook implementations for the Rules module.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\rules\Event\EntityEvent;
use Drupal\rules\Event\UserLoginEvent;
use Drupal\rules\Event\UserLogoutEvent;
use Drupal\rules\Event\SystemCronEvent;
use Psr\Log\LogLevel;
/**
* Implements hook_theme().
*/
function rules_theme() {
return [
'rules_debug_log_element' => [
'render element' => 'element',
'file' => 'rules.theme.inc',
],
];
}
/**
* Implements hook_page_top().
*/
function rules_page_top(array &$page_top) {
$debug_log = \Drupal::service('logger.rules_debug_log');
$markup = $debug_log->render();
// If debugging is turned off $markup will be empty.
if (!empty($markup)) {
if (\Drupal::currentUser()->hasPermission('access rules debug')) {
// Send debug output to the screen.
\Drupal::service('messenger')->addError($markup);
}
// Log debugging information to logger.channel.rules only if the rules
// system logging setting 'debug_log.system_debug' is enabled.
// These logs get sent to the system dblog, end up in the Drupal database,
// and are viewable at /admin/reports/dblog.
if (\Drupal::config('rules.settings')->get('debug_log.system_debug')) {
\Drupal::service('logger.channel.rules')->log(LogLevel::DEBUG, $markup, []);
}
}
// Remove logs already rendered.
$debug_log->clearLogs();
}
/**
* Implements hook_page_attachments().
*/
function rules_page_attachments(array &$attachments) {
// We need JavaScript and CSS to render the debug log properly
// and to provide the expand/collapse functionality.
if (\Drupal::currentUser()->hasPermission('access rules debug')) {
if (\Drupal::config('rules.settings')->get('debug_log.enabled')) {
$attachments['#attached']['library'][] = 'rules/rules.debug';
}
}
}
/**
* Implements hook_help().
*/
function rules_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.rules':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>';
// cspell:ignore gitbooks
$output .= t('The Rules module allows site administrators to define conditionally executed actions based on occurring events (ECA-rules). For more information, see the <a href=":url1" target="_blank">online documentation for the Rules module</a> and the current <a href=":url2" target="_blank">Rules documentation for Drupal 8 on Gitbooks</a>.', [
':url1' => 'https://www.drupal.org/project/rules',
':url2' => 'https://thefubhy.gitbooks.io/rules/content/',
]);
$output .= '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Reaction rules') . '</dt>';
$output .= '<dd>' . t('Reaction rules associate one or more reactions to one or more specific site events. Execution of a reaction rule actions can optionally be tied to one or more conditions. To list and update existing reaction rules and to create a new one, visit the <a href=":url">reaction rules overview page</a>.', [
':url' => Url::fromRoute('entity.rules_reaction_rule.collection')->toString(),
]) . '</dd>';
$output .= '<dt>' . t('Components') . '</dt>';
$output .= '<dd>' . t('Rule components allows to define reusable combined actions which can optionally be tied to one or more conditions. Components are usable as actions in reaction rules or in other components. To list and update existing rule components and to create a new one, visit the <a href=":url">components overview pages</a>.', [
':url' => Url::fromRoute('entity.rules_component.collection')->toString(),
]) . '</dd>';
$output .= '<dt>' . t('General settings') . '</dt>';
$output .= '<dd>' . t('The Rules modules allows to set global settings settings, such as logging. Visit the <a href=":url">rules settings page</a> to view and update current settings.', [
':url' => Url::fromRoute('rules.settings')->toString(),
]) . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_user_login().
*/
function rules_user_login($account) {
// Set the account twice on the event: as the main subject but also in the
// list of arguments.
$event = new UserLoginEvent($account);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, UserLoginEvent::EVENT_NAME);
}
/**
* Implements hook_user_logout().
*/
function rules_user_logout($account) {
// Set the account twice on the event: as the main subject but also in the
// list of arguments.
$event = new UserLogoutEvent($account, [
'account' => $account,
]);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, UserLogoutEvent::EVENT_NAME);
}
/**
* Implements hook_entity_view().
*/
function rules_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Only handle content entities and ignore config entities.
if ($entity instanceof ContentEntityInterface) {
$entity_type_id = $entity->getEntityTypeId();
$event = new EntityEvent($entity, [
$entity_type_id => $entity,
]);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, "rules_entity_view:{$entity_type_id}");
}
}
/**
* Implements hook_entity_presave().
*/
function rules_entity_presave(EntityInterface $entity) {
// Only handle content entities and ignore config entities.
if ($entity instanceof ContentEntityInterface) {
$entity_type_id = $entity->getEntityTypeId();
$event = new EntityEvent($entity, [
$entity_type_id => $entity,
$entity_type_id . '_unchanged' => $entity->original,
]);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, "rules_entity_presave:{$entity_type_id}");
}
}
/**
* Implements hook_entity_delete().
*/
function rules_entity_delete(EntityInterface $entity) {
// Only handle content entities and ignore config entities.
if ($entity instanceof ContentEntityInterface) {
$entity_type_id = $entity->getEntityTypeId();
$event = new EntityEvent($entity, [
$entity_type_id => $entity,
]);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, "rules_entity_delete:{$entity_type_id}");
}
}
/**
* Implements hook_entity_insert().
*/
function rules_entity_insert(EntityInterface $entity) {
// Only handle content entities and ignore config entities.
if ($entity instanceof ContentEntityInterface) {
$entity_type_id = $entity->getEntityTypeId();
$event = new EntityEvent($entity, [
$entity_type_id => $entity,
]);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, "rules_entity_insert:{$entity_type_id}");
}
}
/**
* Implements hook_entity_update().
*/
function rules_entity_update(EntityInterface $entity) {
// Only handle content entities and ignore config entities.
if ($entity instanceof ContentEntityInterface) {
$entity_type_id = $entity->getEntityTypeId();
$event = new EntityEvent($entity, [
$entity_type_id => $entity,
$entity_type_id . '_unchanged' => $entity->original,
]);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, "rules_entity_update:{$entity_type_id}");
}
}
/**
* Implements hook_cron().
*/
function rules_cron() {
$event = new SystemCronEvent();
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch($event, SystemCronEvent::EVENT_NAME);
}
/**
* Implements hook_mail().
*/
function rules_mail($key, &$message, $params) {
$message['subject'] .= str_replace([
"\r",
"\n",
], '', $params['subject']);
$message['body'][] = $params['message'];
}
Functions
Title | Deprecated | Summary |
---|---|---|
rules_cron | Implements hook_cron(). | |
rules_entity_delete | Implements hook_entity_delete(). | |
rules_entity_insert | Implements hook_entity_insert(). | |
rules_entity_presave | Implements hook_entity_presave(). | |
rules_entity_update | Implements hook_entity_update(). | |
rules_entity_view | Implements hook_entity_view(). | |
rules_help | Implements hook_help(). | |
rules_mail | Implements hook_mail(). | |
rules_page_attachments | Implements hook_page_attachments(). | |
rules_page_top | Implements hook_page_top(). | |
rules_theme | Implements hook_theme(). | |
rules_user_login | Implements hook_user_login(). | |
rules_user_logout | Implements hook_user_logout(). |