You are here

function hashtags_clean_orphaned_terms in Hashtags 7

Clean orphaned hashtags in Hashtags vocabulary

Parameters

int $vid vocabulary id:

Return value

int Number of affected values

1 call to hashtags_clean_orphaned_terms()
hashtags_clean_orphaned_terms_submit in ./hashtags.module
Submit handler for 'Clean orphaned hashtags' button

File

./hashtags.module, line 982

Code

function hashtags_clean_orphaned_terms($vid = NULL) {
  if (is_null($vid)) {
    $vid = variable_get('hashtags_vocabulary', '');
  }
  $sql = "SELECT td.tid, (SELECT COUNT(*) \n    FROM {field_data_field_hashtags} fh \n    WHERE fh.field_hashtags_tid = td.tid) AS attached_count \n    FROM {taxonomy_term_data} td \n    WHERE vid = :vid \n    HAVING attached_count = 0";
  $result = db_query($sql, array(
    ':vid' => $vid,
  ));
  foreach ($result as $term) {
    taxonomy_term_delete($term->tid);
  }
  return $result
    ->rowCount();
}