Drupal - What is the appropriate way to get items from an entityqueue?

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.

Tags:

8