View source
<?php
function delete_orphaned_terms_perm() {
return array(
'delete orphaned terms',
);
}
function delete_orphaned_terms_menu() {
$items = array();
$items['admin/settings/delete-orphaned-items'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'delete_orphaned_terms_form',
),
'title' => 'Delete Orphaned Terms',
'description' => "Delete terms with no content associated with them.",
'access arguments' => array(
'delete orphaned terms',
),
);
return $items;
}
function delete_orphaned_terms_form(&$form_state) {
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vocabulary) {
$vocab_names[$vocabulary->vid] = $vocabulary->name . ' (currently ' . count(taxonomy_get_tree($vocabulary->vid)) . ' terms)';
}
$form['dot']['orphaned_terms_vocabs'] = array(
'#type' => 'checkboxes',
'#title' => t('Vocabularies'),
'#description' => t('Select one or more vocabularies to delete orphaned terms from.'),
'#options' => $vocab_names,
'#default_value' => variable_get('orphaned_terms_vocabs', array()),
'#multiple' => TRUE,
);
$form['dot']['orphan_value'] = array(
'#type' => 'textfield',
'#title' => t('Orphan value'),
'#description' => t('Number of nodes associated with a term in order for it to be considered orphaned.'),
'#default_value' => variable_get('orphan_value', 0),
'#max_length' => 1,
'#size' => 1,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function delete_orphaned_terms_form_submit($form, &$form_state) {
variable_set('orphaned_terms_vocabs', $form_state['values']['orphaned_terms_vocabs']);
variable_set('orphan_value', $form_state['values']['orphan_value']);
foreach ($form_state['values']['orphaned_terms_vocabs'] as $vid) {
if ($vid > 0) {
$vocab = taxonomy_vocabulary_load($vid);
$count = delete_orphaned_terms_delete($vid, $form_state['values']['orphan_value']);
drupal_set_message('Previous ' . $vocab->name . ' term count: ' . $count);
}
}
}
function delete_orphaned_terms_delete($vid, $orphan_value) {
$terms = taxonomy_get_tree($vid);
foreach ($terms as $term) {
if (taxonomy_term_count_nodes($term->tid) <= $orphan_value) {
taxonomy_del_term($term->tid);
}
}
return count($terms);
}
function delete_orphaned_terms_help($path, $arg) {
}