1

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?

2
  • 2
    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
    – berramou
    Commented Nov 29, 2021 at 17:12
  • @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.
    – Giuseppe
    Commented Nov 30, 2021 at 11:35

1 Answer 1

8

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.

1
  • In case if site is multilingual and asymmetric workflow for paragraph is used. Use this $node->original->getTranslation($node->language()->getId())->field_paragraph->getValue(); to get the original ids before update
    – Zaid Khan
    Commented Sep 6, 2023 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.