Score:0

EntitySubqueue save() only working with die() following it

ca flag

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();
      }
    }
  }
}
MrD avatar
cn flag
MrD
That mean, you have a loop process. Let check more condition to stop your loop process.
ProGrammar avatar
ca flag
Does it re-save the entity after adding it to the subqueue? Like does it save a reference to the queue on the entity, too? Although updates to nodes shouldn't trigger hook_entity_insert again... This bug has been taking me a lot of time to figure out.
ProGrammar avatar
ca flag
Update: added my full function.
MrD avatar
cn flag
MrD
Let check variable $entity_queue_type_mapping, try with only item 'podcast' => 'everything_else_podcast' and remove line die().
ProGrammar avatar
ca flag
MrD nope... not working either.
berliner avatar
bd flag
Did you check if there are other modules installed which implement `hook_entity_insert` (or similar hooks), that might interfere? As `die()` simply interrupts code execution, it's not limited to your current function, but also prevents any code after your hook invocation from running, so I'd look there.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.