You are here

class ContentEntityTaskManager in Search API 8

Provides a service for managing pending tracking tasks for datasources.

Hierarchy

  • class \Drupal\search_api\Plugin\search_api\datasource\ContentEntityTaskManager implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ContentEntityTaskManager

1 string reference to 'ContentEntityTaskManager'
search_api.services.yml in ./search_api.services.yml
search_api.services.yml
1 service uses ContentEntityTaskManager
search_api.entity_datasource.task_manager in ./search_api.services.yml
Drupal\search_api\Plugin\search_api\datasource\ContentEntityTaskManager

File

src/Plugin/search_api/datasource/ContentEntityTaskManager.php, line 15

Namespace

Drupal\search_api\Plugin\search_api\datasource
View source
class ContentEntityTaskManager implements EventSubscriberInterface {

  /**
   * The Search API task type used by this service for "insert items" tasks.
   */
  const INSERT_ITEMS_TASK_TYPE = 'search_api.entity_datasource.trackItemsInserted';

  /**
   * The Search API task type used by this service for "delete items" tasks.
   */
  const DELETE_ITEMS_TASK_TYPE = 'search_api.entity_datasource.trackItemsDeleted';

  /**
   * The Search API task manager.
   *
   * @var \Drupal\search_api\Task\TaskManagerInterface
   */
  protected $taskManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a ContentEntityTaskManager object.
   *
   * @param \Drupal\search_api\Task\TaskManagerInterface $task_manager
   *   The Search API task manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(TaskManagerInterface $task_manager, EntityTypeManagerInterface $entity_type_manager) {
    $this->taskManager = $task_manager;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events['search_api.task.' . self::INSERT_ITEMS_TASK_TYPE][] = [
      'processEvent',
    ];
    $events['search_api.task.' . self::DELETE_ITEMS_TASK_TYPE][] = [
      'processEvent',
    ];
    return $events;
  }

  /**
   * Processes a datasource tracking event.
   *
   * @param \Drupal\search_api\Task\TaskEvent $event
   *   The task event.
   * @param string $event_name
   *   The name of the event.
   */
  public function processEvent(TaskEvent $event, $event_name) {
    $event
      ->stopPropagation();

    // The complete event name prefix in front of the method name is 45
    // characters long: "search_api.task.search_api.entity_datasource.".
    $method = substr($event_name, 45);
    $task = $event
      ->getTask();
    $index = $task
      ->getIndex();
    $data = $task
      ->getData();
    if (!$index
      ->hasValidTracker()) {
      $args['%index'] = $index
        ->label();
      $message = new FormattableMarkup('Index %index does not have a valid tracker set.', $args);
      $event
        ->setException(new SearchApiException($message));
      return;
    }
    $datasource_id = $data['datasource'];
    $reschedule = FALSE;
    if ($index
      ->isValidDatasource($datasource_id)) {

      /** @var \Drupal\search_api\Plugin\search_api\datasource\ContentEntity $datasource */
      $datasource = $index
        ->getDatasource($datasource_id);
      $raw_ids = $datasource
        ->getPartialItemIds($data['page'], $data['bundles'], $data['languages']);
      if ($raw_ids !== NULL) {
        $reschedule = TRUE;
        if ($raw_ids) {
          $index
            ->startBatchTracking();
          $index
            ->{$method}($datasource_id, $raw_ids);
          $index
            ->stopBatchTracking();
        }
      }
    }
    if ($reschedule) {
      ++$data['page'];
      $this->taskManager
        ->addTask($task
        ->getType(), NULL, $index, $data);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityTaskManager::$entityTypeManager protected property The entity type manager.
ContentEntityTaskManager::$taskManager protected property The Search API task manager.
ContentEntityTaskManager::DELETE_ITEMS_TASK_TYPE constant The Search API task type used by this service for "delete items" tasks.
ContentEntityTaskManager::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ContentEntityTaskManager::INSERT_ITEMS_TASK_TYPE constant The Search API task type used by this service for "insert items" tasks.
ContentEntityTaskManager::processEvent public function Processes a datasource tracking event.
ContentEntityTaskManager::__construct public function Constructs a ContentEntityTaskManager object.