You are here

class GloballinkContinuousEvents in GlobalLink Connect for Drupal 8.2

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

Event subscriber for tmgmt events.

Hierarchy

  • class \Drupal\globallink\EventSubscriber\GloballinkContinuousEvents implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of GloballinkContinuousEvents

1 string reference to 'GloballinkContinuousEvents'
globallink.services.yml in ./globallink.services.yml
globallink.services.yml
1 service uses GloballinkContinuousEvents
globallink.should_create_job in ./globallink.services.yml
Drupal\globallink\EventSubscriber\GloballinkContinuousEvents

File

src/EventSubscriber/GloballinkContinuousEvents.php, line 15

Namespace

Drupal\globallink\EventSubscriber
View source
class GloballinkContinuousEvents implements EventSubscriberInterface {

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

  /**
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  protected $pathMatcher;

  /**
   * GloballinkContinuousEvents constructor.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, PathMatcherInterface $path_matcher) {
    $this->entityTypeManager = $entity_type_manager;
    $this->pathMatcher = $path_matcher;
  }

  /**
   * 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) {
    $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;
        }
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
GloballinkContinuousEvents::$entityTypeManager protected property
GloballinkContinuousEvents::$pathMatcher protected property
GloballinkContinuousEvents::getSubscribedEvents public static function
GloballinkContinuousEvents::onShouldCreateJob public function Do not add the job if we have a filter match.
GloballinkContinuousEvents::__construct public function GloballinkContinuousEvents constructor.