diff --git a/weblinks.module b/weblinks.module index 257fdb4..13d2e13 100644 --- a/weblinks.module +++ b/weblinks.module @@ -136,6 +136,47 @@ function term_get_children_ids($tid) { } /** + * Get table and field information for the vocabulary used in weblinks, which + * may not be the default 'taxonomy_weblinks' field. + * + * @return array + * Array giving the table name and tid field name. + */ +function _weblinks_vocabulary_field_info() { + // Define default to be the standard weblinks vocabulary, so that in the worst + // case, if anything fails below, we fall back to no worse than the previously + // hard-coded table and column name. + // @see http://www.drupal.org/node/2468615 + $vocab_info = array( + 'table_name' => 'field_data_taxonomy_weblinks', + 'tid' => 'taxonomy_weblinks_tid', + ); + if ($vocid = _weblinks_get_vocid()) { + // Get the vocabulary used for weblinks and the fields in the content type. + $weblinks_vocabulary = taxonomy_vocabulary_load($vocid); + $weblinks_fields = field_read_fields(array('bundle' => 'weblinks')); + + // Locate the field which is used for grouping weblinks by looking through + // all the fields that are taxonomy references, and taking the one where the + // 'allowed_values' vocabulary matches the weblinks navigation vocabulary. + foreach ($weblinks_fields as $name => $field_info) { + if ($field_info['type'] == 'taxonomy_term_reference') { + if ($field_info['settings']['allowed_values']['0']['vocabulary'] == $weblinks_vocabulary->machine_name) { + if (isset($field_info['storage']['details'])) { + // FIELD_LOAD_CURRENT has the name of the table and the tid field. + $flc = $field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT']; + $keys = array_keys($flc); + $vocab_info['table_name'] = $keys['0']; + $vocab_info['tid'] = $flc[$keys['0']]['tid']; + } + } + } + } + } + return $vocab_info; +} + +/** * Retrieve taxonomy node count by Term ID. * * @param int $tid @@ -157,12 +198,15 @@ function weblinks_term_node_count($tid, $child_count = TRUE) { $langs = array($language->language); $langs[] = 'und'; - $query = db_select('field_data_taxonomy_weblinks', 't'); - $query->condition('taxonomy_weblinks_tid', $tids, 'IN'); + // Get the table and field name of the vocabulary being used for weblinks. + $vocab_info = _weblinks_vocabulary_field_info(); + + $query = db_select($vocab_info['table_name'], 't'); + $query->condition($vocab_info['tid'], $tids, 'IN'); $query->join('node', 'n', 't.entity_id = n.nid'); $query->condition('n.status', 1); $query->condition('n.type', 'weblinks'); - // Use 'language' from the node table not field_data_taxonomy_weblinks. + // Use 'language' from the node table not field data table. $query->condition('n.language', $langs, 'IN'); $count = $query->countQuery()->execute()->fetchField(); @@ -1497,17 +1541,19 @@ function _weblinks_get_query($tid = 0, $sort = 'title', $limit = 0) { $query->condition('n.status', 1); } - // Join on field_data_taxonomy_weblinks to filter for specific term(s). + // Join on vocabulary data table to filter for specific term(s). if (_weblinks_get_vocid() && $tid !== 'all' && $tid !== 'unpublished' && $sort != 'user') { + // Get the table and field name of the vocabulary being used for weblinks. + $vocab_info = _weblinks_vocabulary_field_info(); if ($tid === 0) { // Select just the unclassified links - those with no matching row. - $query->addJoin('LEFT', 'field_data_taxonomy_weblinks', 't', 't.entity_id = n.nid'); + $query->addJoin('LEFT', $vocab_info['table_name'], 't', 't.entity_id = n.nid'); $query->isNull('t.entity_id'); } else { // Select the rows that match on the required term id(s). - $query->addJoin('INNER', 'field_data_taxonomy_weblinks', 't', 't.entity_id = n.nid'); - $query->where('t.taxonomy_weblinks_tid IN (:tid)', array(':tid' => $tid)); + $query->addJoin('INNER', $vocab_info['table_name'], 't', 't.entity_id = n.nid'); + $query->where('t.' . $vocab_info['tid'] . ' IN (:tid)', array(':tid' => $tid)); } } diff --git a/weblinks.user.inc b/weblinks.user.inc index 0861443..c9b974c 100644 --- a/weblinks.user.inc +++ b/weblinks.user.inc @@ -62,6 +62,9 @@ function weblinks_user_form($form, &$form_state, $account) { $limit = 20; $node_ops = module_invoke_all('node_operations'); + // Get the table and field name of the vocabulary being used for weblinks. + $vocab_info = _weblinks_vocabulary_field_info(); + // Get all Web Links owned by this user. $query = db_select('node', 'n'); $query->extend('PagerDefault') @@ -98,9 +101,9 @@ function weblinks_user_form($form, &$form_state, $account) { // and the field tables will exist. if ($vocid) { // Get the term names associated with this node. - $query = db_select('field_data_taxonomy_weblinks', 't'); - $query->fields('t', array('entity_id', 'taxonomy_weblinks_tid')); - $query->join('taxonomy_term_data', 'tt', 'tt.tid = t.taxonomy_weblinks_tid'); + $query = db_select($vocab_info['table_name'], 't'); + $query->fields('t', array('entity_id', $vocab_info['tid'])); + $query->join('taxonomy_term_data', 'tt', 'tt.tid = t.' . $vocab_info['tid']); $query->fields('tt', array('name')); $query->condition('entity_id', $node->nid); $result = $query->execute();