9

I was having trouble finding API examples for entityqueue in Drupal 8.

I ended up writing a query to get the items.

$query = $this->database->select('entity_subqueue__items', 'esi')
  ->fields('esi', array('items_target_id'))
  ->condition('esi.bundle', $name_of_queue)
  ->orderBy('delta', 'ASC');

$nids = $query->execute()->fetchCol();

But I'd like to use a built in function if possible. Where's the function for retrieving items from an entity queue?

  • The same as in D7 - \Drupal::queue('my queue')->claimItem() – user21641 May 23 '16 at 16:00
  • I don't think that's the same. There's a table queue, that uses the DatabaseQueue object. This is entityqueue module. I tried your example, and it doesn't work, and digging into the code, it looks like DatabaseQueue object is looking at a table with columns such as "name", which are missing from the entity_subqueue__items table. – oknate May 23 '16 at 16:13
  • Ah sorry, I don't use entityqueue module. – user21641 May 23 '16 at 19:07
12

Given $sid is your subqueue id:

$entity_subqueue = \Drupal::entityManager()->getStorage('entity_subqueue')->load($sid);

Loads the subqueue object. You can manipulate this to retrieve or update your entityqueue.

To get the items:

$items = $entity_subqueue->get('items')->getValue();

To update the items, add another item to the $items array and then save the entity object:

$items[] = ['target_id' => 69];

$entity_subqueue->set('items', $items);

$entity_subqueue->save();

Voila!

You can check in the database that your entity queue is updated:

select * from entity_subqueue__items where bundle = :sid;

I would like to also add that there is a great alternative to using entityqueue in Drupal 8: the Config Pages module. It allows you to create entity bundles that have only one entity. You can then add any field type you like, including entity reference fields. I recently replaced an entity queue on a project with a config page with an entity reference field, and I preferred the user experience.

0

There is also a shortcut to load the subqueue: \Drupal\entityqueue\Entity\EntitySubqueue::load($sid);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.