class TestContinuousEventSubscriber

Test event subscriber to test the continuous jobs events.

Hierarchy

Expanded class hierarchy of TestContinuousEventSubscriber

1 file declares its use of TestContinuousEventSubscriber
1 string reference to 'TestContinuousEventSubscriber'
tmgmt_test.services.yml in tmgmt_test/tmgmt_test.services.yml
tmgmt_test/tmgmt_test.services.yml

File

tmgmt_test/src/EventSubscriber/TestContinuousEventSubscriber.php, line 13

Namespace

Drupal\tmgmt_test\EventSubscriber
View source
class TestContinuousEventSubscriber implements EventSubscriberInterface {

  /**
   * Label that prevents a node from being added.
   */
  const DISALLOWED_LABEL = 'Testing SHOULD_CREATE_JOB event';

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * TestContinuousEventSubscriber constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Do not add the job if we have a filter match.
   *
   * @param \Drupal\tmgmt\Events\ShouldCreateJobEvent $event
   *   The event object.
   */
  public function onShouldCreateJob(ShouldCreateJobEvent $event) {

    // Filter out content.
    if ($event
      ->getPlugin() === 'content') {
      $storage = $this->entityTypeManager
        ->getStorage($event
        ->getItemType());
      $entity = $storage
        ->load($event
        ->getItemId());
      if ($entity
        ->label() === static::DISALLOWED_LABEL) {
        $event
          ->setShouldCreateItem(FALSE);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    $events[ContinuousEvents::SHOULD_CREATE_JOB][] = [
      'onShouldCreateJob',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TestContinuousEventSubscriber::$entityTypeManager protected property
TestContinuousEventSubscriber::DISALLOWED_LABEL constant Label that prevents a node from being added.
TestContinuousEventSubscriber::getSubscribedEvents public static function
TestContinuousEventSubscriber::onShouldCreateJob public function Do not add the job if we have a filter match.
TestContinuousEventSubscriber::__construct public function TestContinuousEventSubscriber constructor.