Chris 2pha Brown

Chris Brown

Drupal, Javascript, Three.js, 3D

website blog

Disable taxonomy term delete button if term has nodes

The below snippet will disable the delete button on the taxonomy term edit page if the term has any nodes assigned to it. It implements hook_form_FORM_ID_alter()

function hook_form_taxonomy_form_term_alter(&$form, &$form_state) {
  $query = db_select('taxonomy_index', 't');
  $query->condition('tid', $form_state['term']->tid, '=');
  $query->join('node', 'n', 't.nid = n.nid');

  $count = $query->countQuery()->execute()->fetchField();
  if ($count) {
    $form['actions']['delete']['#disabled'] = TRUE;
    $form['actions']['delete']['#suffix'] = "<span>$count nodes, cant delete</span>";
  }
}