Neuen Kommentar hinzufügen

08 Sep 2014

Prevent deletion of taxonomy term if associated with a node

Why check if term is associated to a node prior to deletion?

In cases where taxonomy terms are used only for categorizing content on a Drupal powered web page, there should be no harm in deleting them. However sometimes taxonomy is used to store terms critical to the content they are referenced from and in this case steps should be taken to prevent an accidental deletion.

I have encountered such a case on a project I am working on which is soon to become a web platform for university students. When creating a faculty node, its name is being defined by choosing a term from the 'faculties' vocabulary. Deleting a term assigned to such a faculty node would lead to... well undesired effects.

Approach

When looking for the right hook you will find that there is no hook_taxonomy_pre_delete and using the existing hook_taxonomy_term_delete would be too late (the term would be deleted by then). (By the way, this problem persists across other entity types, like nodes - hoping to see some added hooks in D8.)

I will describe an easy way of preventing the deletion of a used taxonomy term, but be warned, this will only prevent the deletion of a term in the UI, it will not react to programmatically deleted terms.

Here is how this is going to look like:

Taxonomy term delete warning.

Some code:

  1. // In our custom module 'mymodule' let's go ahead and implement hook_form_alter() (what else!?) and switch on $form_id.
  2. function mymodule_form_alter(&$form, &$form_state, $form_id) {
  3. switch ($form_id) {
  4.  
  5. // This is the general term form.
  6. case 'taxonomy_form_term':
  7.  
  8. // Checking if we are on the delete confirmation form.
  9. if (isset($form['delete'])) {
  10.  
  11. // Getting the term id.
  12. $tid = $form['#term']->tid;
  13.  
  14. // Limiting the query to 30 to save resources when loading the nodes later on.
  15. $limit = 30;
  16.  
  17. // Getting node ids referencing this term and setting a limit.
  18. $result = taxonomy_select_nodes($tid, FALSE, $limit);
  19.  
  20. if (count($result) > 0) {
  21. $markup = t('This term is being used in nodes and cannot be deleted. Please remove this taxonomy term from the following nodes first:') . '<ul>';
  22.  
  23. // Looping through the node ids, loading nodes to get their names and urls.
  24. foreach($result as $nid) {
  25. $node = node_load($nid); // This is quite resource hungry, so if dealing with loads of nodes, make sure you apply a limit in taxonomy_select_nodes().
  26. if (!$node)
  27. continue;
  28. $markup .= '<li>' . l($node->title, 'node/' . $node->nid, array('attributes' => array('target'=>'_blank'))) . '</li>';
  29. }
  30.  
  31. // Appending some text with ellipsis at the end of list in case there might be more nodes referencing this term than the ones displayed.
  32. if (count($result) >= $limit)
  33. $markup .= '<li>' . t("... only the first @limit results are displayed.", array('@limit' => $limit)) . '</li>';
  34. $markup .= '</ul>';
  35.  
  36. // Using the render array's markup key to display our warning message.
  37. $form['description']['#markup'] = $markup;
  38.  
  39. // Removing the 'delete' button from the form.
  40. $form['actions']['submit']['#access'] = FALSE;
  41. // $form['actions']['submit']['#disabled'] = TRUE; // Disables the button instead of removing it.
  42. }
  43. }
  44. break;
  45. }
  46. }

Summary

TL;DR: We want to prevent people deleting taxonomy terms which are already associated to nodes. We use hook_form_alter on the form taxonomy_form_term and use the function taxonomy_select_nodes to check if a node uses the term in question, then we display links to the nodes and remove the 'delete' button.

Feel invited to comment!

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.

Restricted HTML

  • Erlaubte HTML-Tags: <a href hreflang target> <em> <strong> <cite> <blockquote cite> <pre> <ul type> <ol start type> <li> <dl> <dt> <dd> <h4 id> <h5 id> <h6 id>
  • Zeilenumbrüche und Absätze werden automatisch erzeugt.
  • Website- und E-Mail-Adressen werden automatisch in Links umgewandelt.

Angebot innerhalb von 24 Stunden

Ob ein großes kommerzielles System, oder eine kleine Business Seite, wir schicken ein Angebot ab innerhalb von 24 Stunden nachdem Sie diese Taste drücken: Angebot anfordern