View source
<?php
namespace Drupal\scheduler_rules_integration\Event;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\scheduler\SchedulerManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EventDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $schedulerManager;
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;
}
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'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->schedulerManager
->getPluginEntityTypes() as $entity_type_id) {
if ($entity_type_id == 'node') {
continue;
}
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$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(),
]),
],
],
];
$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;
}
}