GloballinkContinuousEvents.php in GlobalLink Connect for Drupal 8
File
src/EventSubscriber/GloballinkContinuousEvents.php
View source
<?php
namespace Drupal\globallink\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\tmgmt\Events\ShouldCreateJobEvent;
use Drupal\tmgmt\Events\ContinuousEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class GloballinkContinuousEvents implements EventSubscriberInterface {
protected $entityTypeManager;
protected $pathMatcher;
public function __construct(EntityTypeManagerInterface $entity_type_manager, PathMatcherInterface $path_matcher) {
$this->entityTypeManager = $entity_type_manager;
$this->pathMatcher = $path_matcher;
}
public function onShouldCreateJob(ShouldCreateJobEvent $event) {
$job = $event
->getJob();
$item_type = $event
->getItemType();
$item_id = $event
->getItemId();
if ($event
->getPlugin() == 'content' && $event
->getJob()
->isContinuous()) {
$storage = $this->entityTypeManager
->getStorage($item_type);
$entity = $storage
->load($item_id);
if (!$entity) {
return;
}
$filters_table = $job
->getSetting('filters_table');
if (empty($filters_table['filters']) || !is_array($filters_table['filters'])) {
return;
}
foreach ($filters_table['filters'] as $filter) {
if (empty($filter['value'])) {
continue;
}
switch ($filter['field']) {
case 'url':
if ($entity
->hasField('path') && $entity
->get('path')->alias && $this->pathMatcher
->matchPath($entity
->get('path')->alias, $filter['value'])) {
$event
->setShouldCreateItem(FALSE);
$job
->addMessage('Item type @type with id @id skipped due to URL starts with filter rule.', [
'@type' => $item_type,
'@id' => $item_id,
], 'debug');
return;
}
break;
case 'id':
if ($filter['value'] == $entity
->id()) {
$event
->setShouldCreateItem(FALSE);
$job
->addMessage('Item type @type with id @id skipped due to URL contains filter rule.', [
'@type' => $item_type,
'@id' => $item_id,
], 'debug');
return;
}
break;
}
}
}
}
public static function getSubscribedEvents() {
$events[ContinuousEvents::SHOULD_CREATE_JOB][] = [
'onShouldCreateJob',
];
return $events;
}
}