0

I built a module that creates a form and let the user to add a variable called unit into database. Now this unit should be the options of select list of a content type. the form id of the content type is phone_book_node_form, but the problem is I can't add options to the select list. this is the firebug output of the form (content type)

enter image description here

the name of the select list field is field_phone_yegan. I tried this function in template.php

function MYTHEME_form_alter(&$form, &$form_state, $form_id){
    switch ($form_id){
        case 'phone_book_node_form':
            $form['field_phone_yegan'] = array(
                '#options' => array('yes'=>t('yes'),'no'=>t('no'))
            );  
            break;              
    }
}

nut nothing was added to the select list. I also tested

$form['edit-field-phone-yegan-und'] = array(
                '#options' => array('yes'=>t('yes'),'no'=>t('no'))
            );  


$form['field_phone_yegan[und]'] = array(
                '#options' => array('yes'=>t('yes'),'no'=>t('no'))
            );  

but it didn't work. How do I can add options to this content type select list?

  • I believe it doesn't work.... It will check for Allowed values list and results in validation error.. I would suggest create a new select field in form alter and handle CRUD operations by creating schema using install file.. – Anil Sagar Apr 26 '14 at 17:07
  • @AnilSagar do you mean to tell me there is no way to add options of a list in a content type? – M a m a D Apr 26 '14 at 17:09
  • You can change the select list option using solution mentioned here by me.. drupal.stackexchange.com/questions/111117/… .. But if you change the existing values it will lead to error if value is not available.. – Anil Sagar Apr 26 '14 at 17:11
1

It sounds like using a taxonomy would be more appropriate here. Taxonomies allow lists and terms can be automatically created from user input, in this example something like the Select (or other) module would be of use. Details from project page:

Provides a new Forms API element which is a select/radios/checkboxes element that has an 'other' option. When 'other' is selected a textfield appears for the user to provide a custom value.

0

I found it, first I should install the cck_select_or_other module , it provides us two usefull textareas:

1. PHP Code

This textarea accepts php code and the php code should return an associative array which is used as options in the field. in my case this code works :

enter image description here

2. Other 

The second, option Other is automatically added in the select list and by clicking on it, a textfield appears and lets you to insert your option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.