I am trying to add an item to a sub-queue automatically on save. And for some reason, it only works if I include die()
afterwards.
This adds the new node to the queue
function custom_module_entity_insert(EntityInterface $entity) {
...
$subqueue = EntitySubqueue::load($entityqueue_id);
$subqueue->addItem($entity)->save();
die();
}
This does NOT add the new node to the queue
function custom_module_entity_insert(EntityInterface $entity) {
...
$subqueue = EntitySubqueue::load($entityqueue_id);
$subqueue->addItem($entity)->save();
}
I've tried putting sleep statements around and can't get it to work without die()
, which breaks the page after saving, obviously. There aren't any helpful messages in the error log. The item saves, it just doesn't get added to the queue without die()
.
Do you have any idea?
This is my working function (and breaks the page)
/**
* Implements hook_entity_insert().
*/
function custom_module_entity_insert(EntityInterface $entity) {
// Only worry about entities that are fieldable.
if ($entity instanceof FieldableEntityInterface) {
if ($entity instanceof NodeInterface &&
in_array($entity->getType(), ['podcast','video','post'])) {
automaticallyAddToEntityqueue($entity);
}
}
}
function automaticallyAddToEntityqueue(EntityInterface $entity) {
$entity_queue_type_mapping = array(
'podcast' => 'everything_else_podcast',
'video' => 'everything_else_video',
'post' => 'everything_else_post'
);
foreach ($entity_queue_type_mapping as $type => $entityqueue_id) {
if ($entity instanceof NodeInterface && $type == $entity->getType()) {
/** @var \Drupal\entityqueue\EntitySubqueueInterface $subqueue */
$subqueue = EntitySubqueue::load($entityqueue_id);
if (method_exists($subqueue, 'addItem')) {
$subqueue->addItem($entity)->save();
die();
}
}
}
}