1 taxonomy_vocabulary.class.inc TaxonomyVocabulary::delete()

Deletes a taxonomy vocabulary.

File

core/modules/taxonomy/taxonomy_vocabulary.class.inc, line 101

Class

TaxonomyVocabulary
Taxonomy vocabularies allow the grouping of taxonomy terms.

Code

function delete() {
  module_invoke_all('taxonomy_vocabulary_predelete', $this);

  // Only load terms without a parent, child terms will get deleted too.
  $tids = db_query('SELECT t.tid FROM {taxonomy_term_data} t INNER JOIN {taxonomy_term_hierarchy} th ON th.tid = t.tid WHERE t.vocabulary = :vocabulary AND th.parent = 0', array(':vocabulary' => $this->machine_name))->fetchCol();
  taxonomy_term_delete_multiple($tids);

  // Load all Taxonomy module fields and delete those which use only this
  // vocabulary.
  $taxonomy_fields = field_read_fields(array('module' => 'taxonomy'));
  foreach ($taxonomy_fields as $field_name => $taxonomy_field) {
    $modified_field = FALSE;
    // Term reference fields may reference terms from more than one
    // vocabulary.
    foreach ($taxonomy_field['settings']['allowed_values'] as $key => $allowed_value) {
      if ($allowed_value['vocabulary'] == $this->machine_name) {
        unset($taxonomy_field['settings']['allowed_values'][$key]);
        $modified_field = TRUE;
      }
    }
    if ($modified_field) {
      if (empty($taxonomy_field['settings']['allowed_values'])) {
        field_delete_field($field_name);
      }
      else {
        // Update the field definition with the new allowed values.
        field_update_field($taxonomy_field);
      }
    }
  }

  field_attach_delete_bundle('taxonomy_term', $this->machine_name);

  module_invoke_all('taxonomy_vocabulary_delete', $this);

  // Delete the configuration file.
  $config = config('taxonomy.vocabulary.' . $this->machine_name);
  $config->delete();

  backdrop_static_reset('taxonomy_vocabulary_load_multiple');
}