You are here

scheduler_rules_integration.module in Scheduler 8

Same filename and directory in other branches
  1. 2.x scheduler_rules_integration/scheduler_rules_integration.module

Scheduler Rules Integration.

This sub-module provides actions, conditions and events for use with the Rules module. All rules code is now moved into this sub-module so that the main Scheduler module does not need Rules as a pre-requisite.

File

scheduler_rules_integration/scheduler_rules_integration.module
View source
<?php

/**
 * @file
 * Scheduler Rules Integration.
 *
 * This sub-module provides actions, conditions and events for use with the
 * Rules module. All rules code is now moved into this sub-module so that the
 * main Scheduler module does not need Rules as a pre-requisite.
 * @see https://www.drupal.org/node/2790459
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\scheduler_rules_integration\Event\ExistingNodeIsScheduledForPublishingEvent;
use Drupal\scheduler_rules_integration\Event\ExistingNodeIsScheduledForUnpublishingEvent;
use Drupal\scheduler_rules_integration\Event\NewNodeIsScheduledForPublishingEvent;
use Drupal\scheduler_rules_integration\Event\NewNodeIsScheduledForUnpublishingEvent;
use Drupal\scheduler_rules_integration\Event\SchedulerHasPublishedThisNodeEvent;
use Drupal\scheduler_rules_integration\Event\SchedulerHasUnpublishedThisNodeEvent;

/**
 * Implements hook_ENTITY_TYPE_insert() for node entities.
 */
function scheduler_rules_integration_node_insert(EntityInterface $node) {

  // Invoke the Rules events to indicate that a new node has been scheduled.
  $scheduler_manager = \Drupal::service('scheduler.manager');
  if (!empty($node->publish_on->value)) {

    // @todo In 7.x we had the dates as parameters. These are available in Rules via node.publish_on.value so maybe we do not need the parms?
    $event = new NewNodeIsScheduledForPublishingEvent($node);
    $scheduler_manager
      ->dispatch($event, NewNodeIsScheduledForPublishingEvent::EVENT_NAME);
  }
  if (!empty($node->unpublish_on->value)) {
    $event = new NewNodeIsScheduledForUnpublishingEvent($node);
    $scheduler_manager
      ->dispatch($event, NewNodeIsScheduledForUnpublishingEvent::EVENT_NAME);
  }
}

/**
 * Implements hook_ENTITY_TYPE_update() for node entities.
 */
function scheduler_rules_integration_node_update(EntityInterface $node) {

  // Invoke Rules events to indicate that an existing node has been scheduled.
  $scheduler_manager = \Drupal::service('scheduler.manager');
  if (!empty($node->publish_on->value)) {
    $event = new ExistingNodeIsScheduledForPublishingEvent($node, [
      'node' => $node,
    ]);
    $scheduler_manager
      ->dispatch($event, ExistingNodeIsScheduledForPublishingEvent::EVENT_NAME);
  }
  if (!empty($node->unpublish_on->value)) {
    $event = new ExistingNodeIsScheduledForUnpublishingEvent($node);
    $scheduler_manager
      ->dispatch($event, ExistingNodeIsScheduledForUnpublishingEvent::EVENT_NAME);
  }
}

/**
 * Trigger Rules events during cron.
 *
 * This function is called from the main Scheduler module publish() and
 * unpublish() functions in the SchedulerManager class.
 */
function _scheduler_rules_integration_dispatch_cron_event(EntityInterface $node, $event_type) {
  $scheduler_manager = \Drupal::service('scheduler.manager');
  if ($event_type == 'publish') {

    // Invoke the event to tell Rules that Scheduler has published this node.
    // @todo 2nd param $publish_on may be needed as the date will no longer be on the node
    $event = new SchedulerHasPublishedThisNodeEvent($node);
    $scheduler_manager
      ->dispatch($event, SchedulerHasPublishedThisNodeEvent::EVENT_NAME);
  }
  elseif ($event_type == 'unpublish') {

    // Invoke the event to tell Rules that Scheduler has unpublished this node.
    // @todo 2nd param $publish_on may be needed as the date will no longer be on the node
    $event = new SchedulerHasUnpublishedThisNodeEvent($node);
    $scheduler_manager
      ->dispatch($event, SchedulerHasUnpublishedThisNodeEvent::EVENT_NAME);
  }
}

Functions