You are here

public function SmartQueue::initSubqueuesCreate in Entityqueue 8

Create initial subqueues based on smartqueue configuration.

File

modules/entityqueue_smartqueue/src/Plugin/EntityQueueHandler/SmartQueue.php, line 259

Class

SmartQueue
Plugin annotation @EntityQueueHandler( id = "smartqueue", title = @Translation("Smart queue"), description = @Translation("Provides automated subqueues for each entity of a given type."), )

Namespace

Drupal\entityqueue_smartqueue\Plugin\EntityQueueHandler

Code

public function initSubqueuesCreate($queue, $entity_ids, $entity_type_id, &$context) {
  $subqueue_ids = $this->entityTypeManager
    ->getStorage('entity_subqueue')
    ->getQuery()
    ->condition('queue', $queue
    ->id())
    ->execute();

  // Add new queues according to configured entity and/or bundle.
  $entities = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->loadMultiple($entity_ids);
  foreach ($entities as $entity) {
    $new_subqueue_id = $queue
      ->id() . '__' . $entity
      ->id();

    // If a relevant subqueue already exists for this queue, continue.
    if (in_array($new_subqueue_id, $subqueue_ids)) {
      continue;
    }

    // Create the subqueue for this entity.
    $subqueue = EntitySubqueue::create([
      'queue' => $queue
        ->id(),
      'name' => $new_subqueue_id,
      'title' => $entity
        ->label(),
      'langcode' => $queue
        ->language()
        ->getId(),
      'attached_entity' => $entity,
    ]);
    $subqueue
      ->save();
    $context['results'][] = $subqueue
      ->id();
    $context['message'] = $this
      ->t('Created subqueue for entity with @id', [
      '@id' => $entity
        ->id(),
    ]);
  }
}