English 中文(简体)
Creating Drupal taxonomy terms from code?
原标题:

What is the function used to create taxonomy terms in Drupal from the code?

最佳回答

Why don t check the API docs? The answer is right there. http://api.drupal.org/api/function/taxonomy_save_term/6

问题回答

A module I was writing needed a specific vocabulary with hierarchical terms. I wrote this function to save the terms:

<?php
/**
 * Save recursive array of terms for a vocabulary.
 *
 * Example:
 * <code><?php
 * $terms = array(
 *    Species  => array(
 *      Dog ,
 *      Cat ,
 *      Bird  ),
 *    Sex  => array(
 *      Male ,
 *      Female  ) )
 * _save_terms_recursive( $vid, $terms );
 * </code>
 *
 * @param int $vid Vocabulary id
 * @param array $terms Recursive array of terms
 * @param int $ptid Parent term id (generated by taxonomy_save_term) 
 */
function _save_terms_recursive( $vid, &$terms, $ptid=0 ) {
  foreach ( $terms as $k => $v ) {
    // simple check for numeric indices (term array without children)
    $name = is_string( $k ) ? $k : $v;
    $term = array(  vid  => $vid,  name  => $name,  parent  => $ptid );
    taxonomy_save_term( $term );
    if ( is_array( $v ) && count( $v ) )
      _save_terms_recursive( $vid, $terms[ $k ], $term[  tid  ] );
  }
}

For Drupal 7, it s taxonomy_term_save(), by the way.

Drupal 7 version looks like this:

/**
 * Save recursive array of terms for a vocabulary.
 *
 * Example of an array of terms:
 * $terms = array(
 *    Species  => array(
 *      Dog ,
 *      Cat ,
 *      Bird  ),
 *    Sex  => array(
 *      Male ,
 *      Female  ) );
 *
 * @param int $vid Vocabulary id
 * @param array $terms Recursive array of terms
 * @param int $ptid Parent term id (generated by taxonomy_save_term, when =0 then no parent)
 *
 * taxonomy_term_save ($term) gives back saved tid in $term
 * 
 **/
function _save_terms_recursively( $vid, &$terms, $ptid=0 ) {
    foreach ( $terms as $k => $v ) {
        // simple check for numeric indices (term array without children)
        $name = is_string( $k ) ? $k : $v;

        $term = new stdClass();
        $term->vid = $vid;
        $term->name = $name;
        $term->parent = $ptid;
        taxonomy_term_save( $term );

        if ( is_array( $v ) && count( $v ) ) {
            _save_terms_recursively( $vid, $terms[ $k ], $term->tid );
        }
    }




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签