You are here

class EventDeriver in Scheduler 2.x

Derives Rules events for all non-node entities supported by Scheduler.

This creates events with names starting with a prefix of "scheduler:" as defined by the property name in scheduler_rules_integration.rules.events.yml, followed by the text in keys of the array $this->derivatives.

The processing below is based on code in the Rules module. For an example see src/Plugin/RulesEvent/EntityUpdateDeriver.php. For backwards compatibility the node event names must remain unchnaged, and this is not possible when using this deriver. Hence the node event names stay written out long-hand in scheduler_rules_integration.rules.events.yml.

Hierarchy

Expanded class hierarchy of EventDeriver

1 string reference to 'EventDeriver'
scheduler_rules_integration.rules.events.yml in scheduler_rules_integration/scheduler_rules_integration.rules.events.yml
scheduler_rules_integration/scheduler_rules_integration.rules.events.yml

File

scheduler_rules_integration/src/Event/EventDeriver.php, line 26

Namespace

Drupal\scheduler_rules_integration\Event
View source
class EventDeriver extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The scheduler manager.
   *
   * @var \Drupal\scheduler\SchedulerManager
   */
  protected $schedulerManager;

  /**
   * Creates a new EventDeriver object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\scheduler\SchedulerManager $scheduler_manager
   *   The scheduler manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, SchedulerManager $scheduler_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
    $this->schedulerManager = $scheduler_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('string_translation'), $container
      ->get('scheduler.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {

    // Get all entity types supported by Scheduler plugins.
    foreach ($this->schedulerManager
      ->getPluginEntityTypes() as $entity_type_id) {

      // Node events are the originals, and for backwards-compatibility those
      // event ids must remain unchanged, which cannot be done with the deriver.
      // So they remain defined in scheduler_rules_integration.rules.events.yml.
      if ($entity_type_id == 'node') {
        continue;
      }
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);

      // Define the values that are the same for all events of this entity type.
      $defaults = [
        'entity_type_id' => $entity_type_id,
        'category' => $entity_type
          ->getLabel() . ' (' . $this
          ->t('Scheduler') . ')',
        'context_definitions' => [
          $entity_type_id => [
            'type' => "entity:{$entity_type_id}",
            'label' => $this
              ->t('The object representing the scheduled @entity_type', [
              '@entity_type' => $entity_type
                ->getLabel(),
            ]),
          ],
        ],
      ];

      // Create six events for this entity type.
      $this->derivatives["new_{$entity_type_id}_is_scheduled_for_publishing"] = [
        'label' => $this
          ->t('After saving a new @entity_type that is scheduled for publishing', [
          '@entity_type' => $entity_type
            ->getSingularLabel(),
        ]),
      ] + $defaults + $base_plugin_definition;
      $this->derivatives["new_{$entity_type_id}_is_scheduled_for_unpublishing"] = [
        'label' => $this
          ->t('After saving a new @entity_type that is scheduled for unpublishing', [
          '@entity_type' => $entity_type
            ->getSingularLabel(),
        ]),
      ] + $defaults + $base_plugin_definition;
      $this->derivatives["existing_{$entity_type_id}_is_scheduled_for_publishing"] = [
        'label' => $this
          ->t('After updating a @entity_type that is scheduled for publishing', [
          '@entity_type' => $entity_type
            ->getSingularLabel(),
        ]),
      ] + $defaults + $base_plugin_definition;
      $this->derivatives["existing_{$entity_type_id}_is_scheduled_for_unpublishing"] = [
        'label' => $this
          ->t('After updating a @entity_type that is scheduled for unpublishing', [
          '@entity_type' => $entity_type
            ->getSingularLabel(),
        ]),
      ] + $defaults + $base_plugin_definition;
      $this->derivatives["{$entity_type_id}_has_been_published_via_cron"] = [
        'label' => $this
          ->t('After Scheduler has published a @entity_type', [
          '@entity_type' => $entity_type
            ->getSingularLabel(),
        ]),
      ] + $defaults + $base_plugin_definition;
      $this->derivatives["{$entity_type_id}_has_been_unpublished_via_cron"] = [
        'label' => $this
          ->t('After Scheduler has unpublished a @entity_type', [
          '@entity_type' => $entity_type
            ->getSingularLabel(),
        ]),
      ] + $defaults + $base_plugin_definition;
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
EventDeriver::$entityTypeManager protected property The entity type manager.
EventDeriver::$schedulerManager protected property The scheduler manager.
EventDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
EventDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
EventDeriver::__construct public function Creates a new EventDeriver object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.