How to Read taxonomy term depth (depth) in drupal

Source: Internet
Author: User
Tags php file php and drupal

Drupal does not provide the dpeth interface for the term by default, but we have other methods to obtain it. We will share two methods in this article. You can choose one that suits your needs.

1: self-built functions are obtained through database query.

Function get_term_depth ($ tid ){
$ Limit = 9;
$ Depth = 1;
While ($ parent = db_query ('select parent FROM {taxonomy_term_hierarchy} WHERE tid =: tid ', array (': tid' => $ tid)-> fetchField ()){
$ Depth ++;
$ Tid = $ parent;
If ($ depth> $ limit ){
Break;
    }
  }
Return $ depth;
}

2. Use ctools

Ctools has a submodule called term_depth. It is estimated that most people have not paid attention to this module. This module provides a hidden function _ term_depth, which can also be used to calculate the depth of a term.

After some exploration, the following provides a method to call this function.

Ctools_plugin_load_function ('ctool', 'access', 'term _ depth ',' _ term_depth ');
$ Tid = 2;
Echo _ term_depth ($ tid );

The following is a prototype of the _ term_depth function provided by ctools. Static variables and recursive functions are used to balance universality and performance.
Function _ term_depth ($ tid ){
Static $ depths = array ();
 
If (! Isset ($ depths [$ tid]) {
$ Parent = db_select ('Taxonomy _ term_hierarchy ', 'th ')
-> Fields ('th', array ('parent '))
-> Condition ('tid', $ tid)
-> Execute ()-> fetchField ();
 
If ($ parent = 0 ){
$ Depths [$ tid] = 1;
    }
Else {
$ Depths [$ tid] = 1 + _ term_depth ($ parent );
    }
  }
 
Return $ depths [$ tid];
}

After completing this article, I found that there is still a third way to get the depth of the term, which is a magic pen; no, the code is as follows:

Ount (taxonomy_get_parents_all ($ tid ));

Official discussion board

Https://www.drupal.org/node/886526


To sum up, if Drupal is used for the same purpose, there are generally at least two solutions available. This is a great challenge for beginners, but once this challenge is completed, it will complete its own gorgeous improvements.




Drupal Taxonomy Term list

Drupal's Taxonomy can add multiple terms to classify content. However, it can also produce the simplest data list.

In Taxonomy, click Add vocabulary to Add a new vocabulary named Category.
Add a Term to Category and name it "News".
Create content creates a content of the Story type.
Because Category vocabulary is added, a Category option is added to the content editing page. When News is selected, the content can be classified into News.

After the preceding process, the access list is available. The Path is taxonomy/term/% (% is the ID value), and the full access address is http://www.domain.com/taxonomy/term/1.

ID is the value of the Term ID. When you can perform the addmenu operation, enter taxonomy/term/1 as the path to display it on the menu.

You can use a template to customize the appearance of the term list. Generally, node is used. tpl. php, if you want to specify a template for an article type, you can use a node-story.tpl.php, story is an article type, if it is a blog, the template is a node-blog.tpl.php. (Note: all content types are node)

The page-node.tpl.php is also a node template, with node. tpl. php is based on node. tpl. php refers to the list page of multiple nodes, and the page-node.tpl.php is the template of a single node, that is, the fullnode or the edit node ).

When used in combination with the UDF, you can add new fields to the content type and output New fields by modifying the template file. In addition, the taxonomy term list has the paging function, which can meet the requirements of most lists.

You can add a new field to Manage fields of content type, and add the UDF field plug-in to the field type. Display fields is used to determine the field rendering mode of the Full node and Teaser pages. (Note: Teaser Is the taxonomyterm page)

If you want to add a custom field to the Teaser page, you can add the mytemplate_preprocess_node function to the template. php of the topic, which can be used to inject code before the output of node. tpl. php and related templates. You can also use the Content Template module to customize the output Template. However, this method is inconvenient for developers and increases the number of modules.

Disadvantage: taxonomy terms can customize fields and rendering methods. It queries data and generates HTML output through the taxonomy_term_page function, but it can only specify TermID and depth, data cannot be customized with more conditions. To obtain a more advanced data list, you can only obtain data using other methods.


Method 1: use SQL to query data, and then use the theme function to call up the template and output it to the page. This method is flexible and efficient, but it will increase a considerable amount of code. If the list is too customized, it is not easy to maintain.


Method 2: Views. Views is a data extraction solution on Drupal, which is quite flexible in data extraction. By default, several Views lists are available. The taxonomy_term is used to replace the List of taxonomyterms. Enabling taxonomy_term makes the node-view-taxonomy_term.tpl.php file available. You can also modify the data sorting by editing views of taxonomyterm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.