1

Is there a way to notify a user when they are about to delete a node that is linked to by another node?

I'm working on a site where many of the pages reference another and I would like to put some mechanism in place where the user is notified before taking action.

  • What do you mean by reference? Do you mean a) if another page has a link (e.g. ahref) to the page you are about to delete or b) if another page has a Drupal reference (e.g. entity reference) to the page you are about to delete? – typologist Oct 23 '15 at 21:12
0

If the node has an entity reference you can use the Rules module.

0

Assuming you are referring to entity references, you could create a custom module (see tutorial), something like this. In this example, the custom module is called "prevent_del." It uses hook_form_ID_alter() The referenced node is of type special_node_type and the referencing nodes are of type page.

function prevent_del_form_node_delete_confirm_alter(&$form, &$form_state, $form_id) {

 if ($form['#node']->type = 'special_node_type') {

    // Get the node id of this special_node_type.
    $nid = $form['nid']['#value'];

    // Get array of nodes of type page referencing this $nid

    $query = new EntityFieldQuery();

    $query->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'page')
      ->propertyCondition('status', NODE_PUBLISHED)
      ->fieldCondition('field_reference', 'target_id', $nid, '=');

    $result = $query->execute();

    // Create a comma-separated string with all nids of referencing nodes
    if (count($result)) {
      $nodes = $result['node'];
      $node_string = array_shift($nodes)->nid;
      foreach ($nodes as $node) {
        $node_string .= ', ' . $node->nid;
      }

      $markup = t('This node is referenced by nodes with ids: @node_string. Please remove the the other nodes before trying to delete this one.', array('@node_string' => $node_string));

      // Use the markup key to display message.
      $form['description']['#markup'] = $markup;

      // Remove the 'delete' button from the form.
      $form['actions']['submit']['#access'] = FALSE;
    }
}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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