2010-03-11 10 views
6

पर चयन/विकल्प सूची जोड़ें मैं थोड़ा उलझन में हूं। मैंने एक टेक्स्ट बॉक्स और सबमिट बटन के साथ एक साधारण रूप बनाया है। अब मैं taxonomy_get_vocabularies() फ़ंक्शन का उपयोग करके वर्गीकरण शर्तों का चयन/विकल्प ड्रॉपडाउन बॉक्स जोड़ना चाहता हूं।ड्रूपल - किसी फॉर्म

$vocabularies = taxonomy_get_vocabularies('my_type'); 

मेरा सवाल यह है कि मैं "ड्रूपल मार्ग" फ़ॉर्म पर शब्दावली सूची कैसे प्राप्त करूं। जिस तरह से ड्रूपल फॉर्म को परिभाषित करता है वह काफी कठोर लगता है। प्रासंगिक वर्गीकरण शर्तों के अस्तित्व पर भी, मैं इस कंडीशन को कैसे बना सकता हूं।

function my_form_name($form_state) { 

// A Short question. 
    $form['title'] = array(
    '#type' => 'textfield', 
    '#title' => t('Question'), 
    '#default_value' => $node->title, 
    '#required' => TRUE, 
    '#weight' => 1, 
    '#description' => t('A text box goes here '), 
); 

    $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('submit'), 
    '#weight' => 7, 
); 

    return $form; 

उत्तर

11
मैं एक कस्टम के रूप में ऐसी ही कुछ कर रहा हूँ

, और यह बहुत आसान समारोह के तर्क के रूप में शब्दावली कोड के साथ, taxonomy_get_tree उपयोग करने के लिए मिल गया। नीचे देखें:

//get the list of locations from taxonomy to use in the dropdown 
$dropdown_source = taxonomy_get_tree(2); 
$dropdown_array = array('0' => '--none--'); 
foreach ($dropdown_source as $item) { 
$key = $item->tid; 
$value = $item->name; 
$dropdown_array[$key] = $value; 
} 

//location filter dropdown 
$form['filterset']['locationfilter'] = array(
    '#weight' => '1', 
    '#key_type' => 'associative', 
    '#multiple_toggle' => '1', 
    '#type' => 'select', 
    '#options' => $dropdown_array, 
    '#title' => 'Filter by location', 
); 

unset($dropdown_array); 
0

जांच कैसे अपने शीघ्र उत्तर के लिए

/** 
* Form builder to list and manage vocabularies. 
* 
* @ingroup forms 
* @see taxonomy_overview_vocabularies_submit() 
* @see theme_taxonomy_overview_vocabularies() 
*/ 
function taxonomy_overview_vocabularies() { 
    $vocabularies = taxonomy_get_vocabularies(); 
    $form = array('#tree' => TRUE); 
    foreach ($vocabularies as $vocabulary) { 
    ... 
0

धन्यवाद वर्गीकरण मॉड्यूल के taxonomy.admin.inc फाइल में यह करने के लिए! मुझे लगता है कि मैंने इसे इस तरह से काम किया है।

$form['limiter'] = array(
    '#type' => 'select', 
    '#title' => t('Choose a value'), 
    '#id' => 'limiter', 
    '#options' => get_faq_terms(), 
); 

function get_faq_terms() { 
    // get the vid value from vocabulary_node_types file 
    $result = db_query("SELECT * FROM vocabulary_node_types WHERE type = 'my_type' "); 
    $node = db_fetch_object($result) ; 
    $vid = $node->vid ; 

    // get corresponding term names from term_data file 
    $items = array(); 
    $terms = taxonomy_get_tree($vid); 
    foreach ($terms as $term) { 
     $count = taxonomy_term_count_nodes($term->tid); 
     if ($count) {  
      $items[$term->tid] = $term->name; 
     } 
    } 
+0

आप पोस्ट के लिए उत्तर के लिए टिप्पणियों का उपयोग करना चाहिए, अतिरिक्त पोस्ट कर नहीं है अपना स्वयं का। – jergason

+0

क्षमा करें, मैंने सोचा कि मेरी टिप्पणी "टिप्पणी" प्रारूप के लिए थोड़ा सा शब्द था। बीटीडब्ल्यू अगर किसी के पास बेहतर समाधान है, तो कृपया हमें बताएं। taxonomy_get_vocabularies() के लिए एक उदाहरण उपयोगी होगा। –

1

इस Drupal तरीका है - _taxonomy_term_select()

+2

ड्रूपल के लिए नहीं 7 – FLY

1

मुझे लगता है कि आप फ़ंक्शन का उपयोग कर सकते हैं: taxonomy_form

+0

<= ड्रोपल 6 केवल – DrCord

2

मैंने:

यहाँ आप doumentation है taxonomy_form मेरे मॉड्यूल के लिए इस सहायक समारोह को लिखा (ड्रूपल 7):

/** 
* helper function to get taxonomy term options for select widget 
* @arguments string $machine_name: taxonomy machine name 
* @return array of select options for form 
*/ 
function MYMODULE_get_tax_term_options($machine_name){ 
    $options = array('0' => ''); 

    $vid = taxonomy_vocabulary_machine_name_load($machine_name)->vid; 

    $options_source = taxonomy_get_tree($vid); 

    foreach($options_source as $item) { 
     $key = $item->tid; 
     $value = $item->name; 
     $options[$key] = $value; 
    } 

    return $options; 
} 

तो फिर आप अपने $ रूप में अपने #options पर इस समारोह कॉल कर सकते हैं:

$form['field_name'] = array( 
    '#options' => MYMODULE_get_tax_term_options('taxonomy_machine_name'), 
); 
1

यहाँ कैसे Drupal में यह करने के लिए 7

// Populate FAPI select box from vocabulary term values. 
// In this case term_reference field is field_category 
$form = array(); 
$form['category_default'] = array(
    '#type' => 'select', 
    '#title' => t('Default category'), 
    '#options' => taxonomy_allowed_values(field_info_field('field_category')), 
    '#description' => t('The selected category will be shown by default on listing pages.') 
); 
return $form; 
संबंधित मुद्दे