Drupal 7 开发内部培训资料, 分头诗人

什么方法可以根据术语表的机器名来获取他下面所有的术语呢?

赞成!
0
否决!

请教一下, 什么方法可以根据术语表的机器名,(或者vid,但我最希望用机器名)来获取他下面所有的术语哦? 我找到这里https://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/7,但不清楚是哪个。谢谢呢!

3 个回答

赞成!
0
否决!
/**
 * Helper function to dynamically get the tid from the term_name
 *
 * @param $term_name Term name
 * @param $vocabulary_name Name of the vocabulary to search the term in
 *
 * @return Term id of the found term or else FALSE
 */
function _get_term_from_name($term_name, $vocabulary_name) {
  if ($vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name)) {
    $tree = taxonomy_get_tree($vocabulary->vid);
    foreach ($tree as $term) {
      if ($term->name == $term_name) {
        return $term->tid;
      }
    }
  }
  return FALSE;
}
赞成!
0
否决!
function catelog($parent = 0)
{
	global $language;
	$output = '<ul>';
	$query = db_select('taxonomy_term_data','td');
	$query->leftJoin('taxonomy_term_hierarchy','th','td.tid = th.tid');
	$query->fields('td')->fields('th', array('parent'))
		->condition('td.vid', '2')->condition('td.language', $language->language)->condition('th.parent', $parent)->orderBy('weight', 'ASC')->orderBy('tid', 'DESC');
	$query = $query->execute()->fetchAll();
	foreach ($query as $ob)
	{
		$output .= '<li>';
		$output .= l($ob -> name, 'taxonomy/term/'.$ob -> tid);
		$p = db_select('taxonomy_term_hierarchy', 't')->fields('t')->condition('t.parent', $ob -> tid)->execute();
		$num_of_results = $p->rowCount();
		//$output .= ' ('.$num_of_results.')';
		if($num_of_results > 0){
			$output .= catelog($ob -> tid);
		}
		$output .= '</li>';
	}	
	$output .= '</ul>';
	return $output;
}

自己写个函数就可以了

赞成!
0
否决!

嗯,其实我很不喜欢重复造轮子的。如果有现成的API,就直接调用,没有的话,就自己写了。

呵呵,谢谢楼上两位热心解答的朋友。