1

I have a Content Type called ItemRequest, and another Content Type called ItemOrder. One of the fields in ItemOrder is "Related Item Request" that is an Entity Reference to ItemRequest. Each ItemOrder has at least one Related Item Request.

How do I ensure that a parent ItemRequest node can only be deleted if it does not have any ItemOrder nodes relating to it? What hook can I use that can test for the existence of these child nodes and call form_set_error to abandon node deletion?

1 Answer 1

1

You can use hook_node_access:

Modules may implement this hook if they want to have a say in whether or not a given user has access to perform a given operation on a node.

The second parameter for this hook is $op:

$op: The operation to be performed. Possible values:

"create" "delete" "update" "view"

And the return values are:

  • NODE_ACCESS_ALLOW: if the operation is to be allowed.
  • NODE_ACCESS_DENY: if the operation is to be denied.
  • NODE_ACCESS_IGNORE: to not affect this operation at all.

So, you should implement that hook, check if the node being deleted is of type ItemRequest and if yes collect all the ItemOrder that point to it. If none are collected you can return NODE_ACCESS_IGNORE, if there're ItemOrder pointing to the ItemRequest return NODE_ACCESS_DENY.

While you can hook into the node form and add a custom validator that doesn't prevent user to delete the node using other means (admin content view, for example). This method can prevent any action trying to delete a node.

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.