Score:1

How to hook on paragraph deletion?

br flag

I'm on D9 and I've a paragraph inside a content type node.

I need to hook when the paragraph is deleted to do some custom logic and set up a queue.

I've tried hook_entity_delete, hook_entity_predelete or hook_paragraph_delete, hook_paragraph_predelete: those hooks run during cron, if the node containing the paragraph is deleted.

However, it seems to me those aren't triggered if an existing paragraph is deleted via the widget inside the edit form of the node.

Is there a way to intercept that case/solve my problem?

berramou avatar
gb flag
when you remove the paragraph from content you delete the reference not the paragraph item, so it's normal that none of those hooks is triggered
Giuseppe avatar
br flag
@berramou That's true, but I expected they would trigger during cron, because I supposed paragraphs with no references were deleted. However after checking the DB effectively they don't seems to be deleted.
Score:6
cn flag

You need to implement a node update hook and compare the paragraph IDs:

use Drupal\node\Entity\NodeInterface;

/**
 * Implements hook_ENTITY_TYPE_update() for node entities.
 */
function mymodule_node_update(NodeInterface $node) {
  if ($node->getType() == 'my_content_type') {
    $ids = array_column($node->field_paragraph->getValue(), 'target_id');
    $original_ids = array_column($node->original->field_paragraph->getValue(), 'target_id');
    // compare IDs
  }
}

If this is a nested paragraph then use hook_paragraph_update() for the parent paragraph type.

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.