2012-02-13 24 views
5

मैं शीर्षक = "आइकन/icon_cart.gif" जोड़ना चाहता हूं, जो मेरी चयन सूची में निम्न विकल्पों में से प्रत्येक के लिए दृश्यों का उपयोग करके प्रस्तुत किया गया है।फॉर्म एपीआई का उपयोग करके विकल्प तत्व में विशेषता जोड़ना: ड्रूपल 7

कई लेखों को आजमाने और पढ़ने के बाद मुझे इस फ़ॉर्म को मेरे रूप में जोड़ने का तरीका नहीं दिख रहा है।

नीचे मेरा कोड है।

function customchatter_form_alter(&$form, &$form_state, $form_id) { 

$form["tid"]["#options"][1]=t("nooo chatter"); 
// this works to change the label of the option but how do I add title="icons/icon- 
cart.gif" ? 

} 

मेरे एचटीएमएल कोड:

<select id="edit-tid" name="tid" class="form-select"> 
<option value="All">- Any -</option> 
<option value="1">nooo chatter</option> 
<option value="2">Complaints Complaints</option> 
<option value="3">Gear &amp; Gadgets</option> 
</select> 

चीयर्स, विशाल

अद्यतन मैं क्लाइव की सलाह के अनुसार के रूप में कोड को अद्यतन करने की कोशिश की लेकिन मूल्यों अभी भी ठीक नहीं आ रहे हैं। नीचे मेरी कहानी है।

तो नीचे एचटीएमएल उत्पादन मैं प्राप्त करने के लिए कर रहा हूँ है, लेकिन शीर्षक नहीं है लेकिन मान गलत है के रूप में आप देख सकते हैं शीर्षक हमेशा नंबर 1.

<select id="edit-select" class="form-select" name="select"> 
<option value="1" title="1">One</option> 
<option value="2" title="1">Two</option> 
</select> 

हो रहा है। नीचे मेरा रूप और कार्य मैंने लिखा है।

मेरे प्रपत्र:

$form['select'] = array(
'#type' => 'select', 
'#options' => array(1 => 'One', 2 => 'Two'), 
'#title' => array(1 => 'One', 2 => 'Two') 
// I tried many combinations but nothing seems to work. 
); 

मेरे विषय कार्य करता है।

function kt_vusers_select($variables) { 
$element = $variables['element']; 
element_set_attributes($element, array('id', 'name', 'size')); 
_form_set_class($element, array('form-select')); 

return '<select' . drupal_attributes($element['#attributes']) . '>' .  
kt_vusers_form_select_options($element) . '</select>'; 
} 


function kt_vusers_form_select_options($element, $choices = NULL) { 

if (!isset($choices)) { 
$choices = $element['#options']; 
} 
// array_key_exists() accommodates the rare event where $element['#value'] is NULL. 
// isset() fails in this situation. 
$value_valid = isset($element['#value']) || array_key_exists('#value', $element); 


// @vishal so there I have declared the variable to accept the values. 
$vtitle = isset($element['#title']) || array_key_exists('#title', $element); 


$value_is_array = $value_valid && is_array($element['#value']); 
$options = ''; 
foreach ($choices as $key => $choice) { 
if (is_array($choice)) { 
    $options .= '<optgroup label="' . $key . '">'; 
    $options .= form_select_options($element, $choice); 
    $options .= '</optgroup>'; 
} 
elseif (is_object($choice)) { 
    $options .= form_select_options($element, $choice->option); 
} 
else { 
    $key = (string) $key; 
    if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || 
($value_is_array && in_array($key, $element['#value'])))) { 
    $selected = ' selected="selected"'; 
    } 
    else { 
    $selected = ''; 
    } 

// @vishal this is where the variable is being used. 

$options .= '<option title="'.$vtitle.'" value="' . check_plain($key) . '"' . $selected . 
'>' . check_plain($choice) . '</option>'; 
} 
} 
return $options; 
} 
नीचे

पिछले विषय समारोह के लिए सही कोड है

function kt_vusers_form_select_options($element, $choices = NULL, $vtitles=NULL) { 
// Build up your own version of form_select_options here 
// that takes into account your extra attribute needs. 
// This will probably involve inspecting your custom FAPI property, 
// which we'll call #extra_option_attributes 

if (!isset($choices)) { 
$choices = $element['#options']; 
$vtitles = array(); 
$vtitles = $element['#title']; 
} 
// array_key_exists() accommodates the rare event where $element['#value'] is NULL. 
// isset() fails in this situation. 
$value_valid = isset($element['#value']) || array_key_exists('#value', $element); 

$value_is_array = $value_valid && is_array($element['#value']); 
$options = ''; 

// print_r($vtitles); 

जबकि ( (सूची ($ कुंजी, $ विकल्प) = प्रत्येक ($ विकल्प)) & & (सूची ($ keytwo, $ vtitle) = प्रत्येक ($ vtitles)) ) { // printf ("% s =>% s,% s =>% s \ n", $ key1, $ value1, $ key2, $ मान 2);

if (is_array($choice)) { 
    $options .= '<optgroup label="' . $key . '">'; 
    $options .= kt_vusers_form_select_options($element, $choice); 
    $i++; 
    // $options .= form_select_options($element, $vtitle); 
    $options .= '</optgroup>'; 
    } // end if if is_array 

elseif(is_object($choice)) { 
    $options .= form_select_options($element, $choice->option); 
    } // end of else if 



else { 
     $key = (string) $key; 
    if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || 
($value_is_array  && in_array($key, $element['#value'])))) { 
    $selected = ' selected="selected"'; 
    } 
    else { 
    $selected = ''; 
    } 
    // $options .= '<option title="'.$vtitle.'" value="' . check_plain($key) . '"' . 
    $selected . '>' . check_plain($choice) . '</option>'; 


} 

$options .= '<option value="'. check_plain($key) .'" title="' . $vtitle . '"' . $selected 
.'>'. check_plain($choice) .'</option>'; 



} // end of choice 



return $options; 


} // end of function 
+0

उनके बारे में यह एक खुला मुद्दा है https://drupal.org/node/342316 – gagarine

उत्तर

11

मुझे डर है कि आपको ऐसा करने के लिए बहुत दूर खोदना होगा, #options सरणी form_select_options() में फ़्लैटन हो गई है और इसमें वर्तमान में विशेषता जोड़ने का कोई तरीका शामिल नहीं है।यह कोड है:

$options .= '<option value="' . check_plain($key) . '"' . $selected . '>' . check_plain($choice) . '</option>'; 

जैसा कि आप देख सकते हैं कि वहां विशेषताओं के लिए कोई गुंजाइश नहीं है। हालांकि आप इसे ओवरराइड करने में सक्षम होंगे, लेकिन इसमें theme_select() और अपनी स्वयं की एफएपीआई संपत्ति का अपना संस्करण लागू करना शामिल होगा।

मैं पूरी बात बाहर लेकिन अपने विषय फ़ाइल में आप कुछ इस तरह कर रहे होते मांस के लिए समय नहीं मिला है: MYTHEME_form_select_options

और में अपने कोड के निर्माण के लिए

function MYTHEME_select($variables) { 
    $element = $variables['element']; 
    element_set_attributes($element, array('id', 'name', 'size')); 
    _form_set_class($element, array('form-select')); 

    return '<select' . drupal_attributes($element['#attributes']) . '>' . MYTHEME_form_select_options($element) . '</select>'; 
} 


function MYTHEME_form_select_options($element) { 
    // Build up your own version of form_select_options here 
    // that takes into account your extra attribute needs. 
    // This will probably involve inspecting your custom FAPI property, 
    // which we'll call #extra_option_attributes 
} 

form_select_options का संदर्भ लें प्रपत्र होगा कुछ की तरह:

$form['select'] = array(
    '#type' => 'select', 
    '#options' => array(1 => 'One', 2 => 'Two'), 
    '#extra_option_attributes' => array(
    1 => array('title' => 'Test title'), // Attributes for option with key "1", 
    2 => array('title' => 'Test title'), // Attributes for option with key "2", 
) 
); 

MYTHEME_form_select_options() में आप तो तत्व केनिरीक्षण कर सकते हैंकुंजी (यदि कोई मौजूद है) यह देखने के लिए कि क्या आपको अपने द्वारा बनाए गए HTML में भौतिक रूप से कोई और विशेषता जोड़ने की आवश्यकता है या नहीं।

आशा है कि मदद करता है, मुझे पता है कि यह आपको जो करने की ज़रूरत है उसे करने के लिए एक पागलपन से लंबे समय तक चलने वाला तरीका लगता है लेकिन जहां तक ​​मुझे पता है कि यह एकमात्र तरीका है।

+0

आप इस दौर @Clive जीतते हैं। आप इस दौर को जीतते हैं। प्रयास के लिए +1। – SpaceBeers

+0

धन्यवाद क्लाइव। मैं इसे एक शॉट दूंगा और वापस लौटा दूंगा। –

+0

क्लाइव मैंने पोस्ट को अपडेट किया है, मैं HTML में शीर्षक प्राप्त करने में सक्षम हूं लेकिन मान हमेशा एक प्रतीत होता है। –

0

Untested, लेकिन आप की कोशिश की है:

$form["tid"]["#options"][1]['#attributes'] = array('title' => t('YOUR NEW TITLE')); 

आप तत्व आदि के नाम लेकिन #attributes टैग काम करना चाहिए के साथ चारों ओर गंदगी की जरूरत हो सकती।

संपादित करें: के रूप में क्लाइव के जवाब में बताया, यह काम नहीं करेगा, लेकिन मैं इसे छोड़ देंगे में मामला किसी को भी अन्य क्षेत्रों (बक्सें आदि) के लिए विशेषताओं को जोड़ने के लिए कैसे जानना चाहता है।

+0

कोशिश करने के लिए धन्यवाद। चीयर्स, –

+2

+1 क्योंकि मुझे नहीं लगता कि एक ऐसे उत्तर पर नकारात्मक प्रतिनिधि है जो अच्छे विश्वास में था उचित है! –

+0

आप एक सज्जन हैं। – SpaceBeers

0

परीक्षण

प्रयास करें:

$form["tid"][1]['#attributes'] = array('title' => t('nooo chatter')); 
बजाय

:

$form["tid"]['#options'][1]['#attributes'] = array('title' => t('nooo chatter'));

यह इस तरह से तत्वों के लिए मनमाने ढंग से विशेषताओं को जोड़ने के लिए संभव है। मैंने this उत्तर के आधार पर कुछ परीक्षण करने के बाद यह खोज लिया।

ने भी here और here का उल्लेख किया।

+0

यह मेरे लिए काम नहीं करता है, यह उन्हें विकल्प सरणी के बाहर जोड़ा गया, इसलिए प्रत्येक तत्व के लिए कुछ भी प्रस्तुत नहीं किया गया। – Collins