2012-02-13 5 views
5

Desidero aggiungere title = "icons/icon_cart.gif" per ciascuna delle seguenti opzioni nella mia lista di selezione che viene visualizzata tramite visualizzazioni.Aggiunta di attributo all'elemento opzione utilizzando api di moduli: drupal 7

Dopo aver provato e letto molti articoli, non riesco a trovare il modo di aggiungere questo html nel mio modulo.

Di seguito è riportato il mio codice.

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

} 

mio codice html:

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

Cheers, Vishal

UPDATE ho cercato di aggiornare il codice di cui al consiglio di Clive, ma i valori non sono ancora arrivando a destra. Di seguito è la mia storia.

Così sotto è l'output HTML sono in grado di realizzare, ma il titolo sembra sempre di essere il numero 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> 

Come si può vedere il titolo è lì, ma il valore è sbagliato. Di seguito è la mia forma e le funzioni che ho scritto.

La mia forma:

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

mie funzioni a tema.

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

sotto è il codice corretto per l'ultima funzione tema

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

mentre ( (lista ($ chiave, $ scelta) = ciascuno ($ scelte)) & & (lista ($ keytwo, $ vtitle) = each ($ vtitles)) ) { // printf ("% s =>% s,% s =>% s \ n", $ chiave1, $ valore1, $ chiave2, $ value2);

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

Il loro è un problema aperto su questo https://drupal.org/node/342316 – gagarine

risposta

11

Ho paura che tu debba scavare molto in basso per fare questo, l'array #options è appiattito in form_select_options() e al momento non include alcun modo di aggiungere attributi.Questo è il codice:

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

Come potete vedere non c'è semplicemente spazio per gli attributi in là. Sarai in grado di sovrascriverlo, ma ciò comporterà l'implementazione della tua versione di theme_select() e della tua proprietà FAPI.

io non ho tempo per rimpolpare l'intera cosa fuori, ma nel file tema si sarebbe fare qualcosa di simile:

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 
} 

Fare riferimento alla form_select_options per la costruzione MYTHEME_form_select_options

E il codice in la forma sarebbe qualcosa di simile:

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

In MYTHEME_form_select_options() si può quindi ispezionare l'elemento delChiave(se ne esiste una) per vedere se è necessario aggiungere fisicamente altri attributi nell'HTML che si crea.

Spero che questo aiuti, so che sembra un modo follemente prolisso di fare ciò che è necessario, ma per quanto ne so è l'unico modo.

+0

Hai vinto questo round @Clive. Hai vinto questo round. +1 per lo sforzo. – SpaceBeers

+0

grazie clive. Darò un colpo e tornerò indietro. –

+0

clive Ho aggiornato il post Sono in grado di ottenere il titolo lì in html ma il valore sembra sempre essere uno. –

0

testato, ma hai provato:

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

si potrebbe aver bisogno di pasticciare con il nome dell'elemento, ecc, ma il tag #attributes dovrebbe funzionare.

MODIFICA: Come indicato nella risposta di Clive, questo non funzionerà, ma lo lascerò nel caso in cui qualcuno voglia sapere come aggiungere attributi ad altri campi (caselle di testo, ecc.).

+0

grazie per aver provato. evviva, –

+2

+1 perché non penso che avere una risposta negativa in risposta in buona fede sia giusto! –

+0

Sei un gentiluomo. – SpaceBeers

0

testato

Prova:

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

anziché:

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

È possibile aggiungere attributi arbitrari di elementi in questo modo. L'ho scoperto dopo aver eseguito alcuni test basati sulla risposta this.

Inoltre citato here e here.

+0

Questo non ha funzionato per me, li ha aggiunti al di fuori dell'array delle opzioni, quindi niente renderizzato per ogni elemento. – Collins

Problemi correlati