Is there a way/function to programmatically delete an item from any queue or maybe an specific queue? For nodequeue I used nodequeue_node_delete() but I don't know what's the equivalent for entityqueue. Thanks.

CommentFileSizeAuthor
#2 2982689.patch1.3 KBamateescu
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

zietbukuel created an issue. See original summary.

amateescu’s picture

Title: How to programmatically remove an item from a queue? » Add a way to programmatically remove an item from a queue
Category: Support request » Feature request
Status: Active » Needs review
FileSize
1.3 KB

Can you try this function?

/**
 * Removes an item from a subqueue.
 *
 * @param \EntitySubqueue $subqueue
 *   An entity subqueue.
 * @param string|int $reference_id
 *   An entity ID to remove from the subqueue.
 */
function entityqueue_subqueue_remove_item(EntitySubqueue $subqueue, $reference_id) {
  // Get the corresponding queue for this subqueue.
  $queue = entityqueue_queue_load($subqueue->queue);
  $field_name = _entityqueue_get_target_field_name($queue->target_type);

  // Filter the field items and remove the referenced ID.
  $field_items = field_get_items('entityqueue_subqueue', $subqueue, $field_name);
  $field_items = array_filter($field_items, function($value) use ($reference_id) {
    return ($value['target_id'] != $reference_id);
  });

  // Set the filtered item list on the subqueue.
  $langcode = field_language('entityqueue_subqueue', $subqueue, $field_name, NULL);
  $subqueue->{$field_name}[$langcode] = $field_items;

  $subqueue->save();
}
lukasss’s picture

Looks good!

zietbukuel’s picture

@amateescu would this work for D7?

amateescu’s picture

@zietbukuel #2 only works for D7 :) In D8 it would be much easier to do..

Give it a try and if it works for you, I'll commit it to the 7.x-1.x branch.

lukasss’s picture

This is work!

zietbukuel’s picture

Yes, I can confirm this works fine.

  • amateescu committed 3f8f057 on 7.x-1.x
    Issue #2982689 by amateescu: Add a way to programmatically remove an...
amateescu’s picture

Version: 7.x-1.5 » 8.x-1.x-dev
Status: Needs review » Patch (to be ported)

Thanks for testing! Committed and pushed to 7.x-1.x.

Moving to 8.x now :)

amateescu’s picture

Status: Patch (to be ported) » Fixed

The ability to remove an item programmatically was added to 8.x in #2145441: Add an Entityqueue tab on entity pages.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.