2012-02-13 22 views
5

Görünümler listemde görüntülenen aşağıdaki seçeneklerin her biri için title = "icons/icon_cart.gif" eklemek istiyorum.Form api kullanarak seçenek öğesine özellik ekleme: drupal 7

Birçok makaleyi denedikten ve okuduktan sonra, bu html'yi formumun içine ekleme yolunu bulamıyorum.

Kodum aşağıda.

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" ? 

} 

Benim html kodu:

<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> 

Alkış, Vishal

GÜNCELLEME Ben Clive'nin ihbar işareti kodunu güncellemek için çalıştı ancak değerler hala hemen geliyor verilmez. Aşağıda benim hikayem.

Yani aşağıdaki I elde etmek mümkün duyuyorum html çıktısı ancak, başlık var ama değeri yanlış Gördüğünüz gibi başlık her zaman numaralı 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> 

gibi görünüyor. Aşağıda benim formum ve yazdığım işlevler.

Benim formu:

$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. 
); 

My Tema fonksiyonları. Aşağıda

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; 
} 

son tema fonksiyonu için doğru kodu

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); 

( (listede ($ anahtarı $ seçim) = her ($ seçimler)) & & (liste ederken ($ keytwo, $ vtitle) = her ($ vtitles)) ) { // printf ("% s =>% s,% s =>% s \ n", $ key1, $ değer1, $ key2, $ değer2);

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

Bunlar, https://drupal.org/node/342316 – gagarine

cevap

11

Korkarım ki bunu yapmak için oldukça fazla kazmak zorunda kalacaksınız, #options dizisi form_select_options() içinde düzleştirilmiş ve şu anda özellik eklemenin herhangi bir yolunu içermiyor.

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

orada öznitelikleri için hiçbir kapsamı sadece orada görebileceğiniz gibi: Bu kodudur. Bununla birlikte, bunu geçersiz kılabilirsiniz, ancak kendi sürümünüz olan theme_select() ve kendi FAPI özelliğinizin uygulanmasını içerecektir.

Dışarı ama tema dosyasında böyle bir şey yapıyor olacak tüm şey eti için zamanım yok

: MYTHEME_form_select_options

Ve kodunu oluşturmak için

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 bakın MYTHEME_form_select_options() yılında

$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", 
) 
); 

sonra eleman eninceleyebilir: gibi bir şey formu olabilir Oluşturduğunuz HTML'ye başka nitelikler eklemeniz gerekip gerekmediğini görmek içinanahtarı (varsa).

Yardımcı olabileceğine inanıyorum, ihtiyacım olanı yapmanın acımasız bir şekilde uzun soluklu bir yolu gibi göründüğünü biliyorum ama bildiğim kadarıyla tek yol bu.

+0

Bu turu @Clive kazanırsınız. Bu turu sen kazandın. Çaba için +1. – SpaceBeers

+0

teşekkürler uçurum. Bir atış yapacağım ve geri döneceğim. –

+0

clive Bu yazıyı güncelledim, başlığı html'de alabiliyorum ama değer hep bir tane gibi görünüyor. –

0

Untestedama denedi:

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

Arkanı eleman vb adına ama çalışması gerekir #attributes etiketiyle karmaşa gerekebilir.

DÜZENLEME: gibi bu işe yaramaz ama durum herkes başka alanlarda (metin kutularının vb) öznitelikler eklemek için nasıl bilmek istiyor ben onu bırakacağım Clive'nin yanıtında dikkat çekti.

+0

denediğiniz için teşekkürler. neşeliler, –

+2

+1 çünkü iyi niyetle yapılmış bir cevabın negatif olduğunu düşünmüyorum adil! –

+0

Siz bir centilmensiniz. – SpaceBeers

0

Test

Dene: yerine

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

:

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

Böylesi elemanlarına keyfi özelliklerini eklemek mümkündür. Bunu, this cevabına dayanarak bazı testler yaptıktan sonra keşfettim. Ayrıca, here ve here'dan da bahsedilmektedir.

+0

Bu benim için işe yaramadı, bu bunları, seçenekler dizisinin dışında ekledik, böylece her öğe için hiçbir şey oluşturulmadı. – Collins