You are here

public function GloballinkContinuousEvents::onShouldCreateJob in GlobalLink Connect for Drupal 8.2

Same name and namespace in other branches
  1. 8 src/EventSubscriber/GloballinkContinuousEvents.php \Drupal\globallink\EventSubscriber\GloballinkContinuousEvents::onShouldCreateJob()

Do not add the job if we have a filter match.

Parameters

\Drupal\tmgmt\Events\ShouldCreateJobEvent $event: The event object.

File

src/EventSubscriber/GloballinkContinuousEvents.php, line 41

Class

GloballinkContinuousEvents
Event subscriber for tmgmt events.

Namespace

Drupal\globallink\EventSubscriber

Code

public function onShouldCreateJob(ShouldCreateJobEvent $event) {
  $job = $event
    ->getJob();
  $item_type = $event
    ->getItemType();
  $item_id = $event
    ->getItemId();

  // Filter out content.
  if ($event
    ->getPlugin() == 'content' && $event
    ->getJob()
    ->isContinuous()) {
    $storage = $this->entityTypeManager
      ->getStorage($item_type);

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $storage
      ->load($item_id);
    if (!$entity) {
      return;
    }

    /** @var array $filters_table */
    $filters_table = $job
      ->getSetting('filters_table');
    if (empty($filters_table['filters']) || !is_array($filters_table['filters'])) {
      return;
    }
    foreach ($filters_table['filters'] as $filter) {

      // @todo Find a way to not save settings without value.
      if (empty($filter['value'])) {
        continue;
      }

      // Url filter.
      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;
      }
    }
  }
}