0

I'm making a module where the content type and fields are created by install. Now i want to make one of those fields only to show up when an option is selected. I did this with AJAX, in some way I can't make it work with the field as defined in the module.install file. I have a radio button selector for two options. When selecting the first option there should show up a selectbox with a list of nodes to select. When selecting the second option two textfields should be shown instead. When I try to add content of this type I get the error message (when changing the value from the radio buttons):

An illegal choice has been detected. Please contact the site administrator.

Can someone give me some advice to make this work? Thanks in advance!


Here is my code from the install file:

function _concert_installed_fields() {
$t = get_t();
return array(
  // other fields left out
'venue_node' => array(
 'field_name' => 'venue_node',
 'label' => 'Select a venue from the list',
 'type' => 'list_integer',
 'cardinality' => '1',
 'foreign keys' => array(),
 'indexes' => array(
   'value' => array(
    0 => 'value',
   ),
  ),
 'module' => 'list',
 'settings' => array(
  'allowed_values_function' => '_concert_content_type_venue_options_list',
  ),
),
'venue_radio' => array(
 'field_name' => 'venue_radio',
 'cardinality' => 1,
 'type' => 'list_boolean',
)
);

From my .module file:

 function concert_form_alter(&$form, &$form_state, $form_id) {
 if($form_id == "concert_node_form") {
 $form['venue_radio'] = array(
  '#title' => t("Choose a venue from the database or add one manually"),
  '#type' => 'radios',
  '#options' => array(0 => t('Add a venue from the database.'),
                      1 => t('Add a venue manually.'),),
  '#ajax' => array(
    'callback' => 'concert_venue_callback',
    'wrapper' => 'venue_select',
    'effect' => 'fade',
  ),
);

$form['venue_select'] = array(
  '#prefix' => '<div id = "venue_select">',
  '#suffix' => '</div>',
  '#type' => 'fieldset',
);
if (!empty($form_state['values']['venue_radio']) && $form_state['values']['venue_radio']   == "1") {
  $form['venue_node']['#type'] = 'hidden';

  $form['venue_select']['location'] = array(
    '#type' => 'textfield',
    '#title' => t('location'),

  );
  $form['venue_select']['venue_city'] = array(
    '#type' => 'textfield',
    '#title'=> t('city'),
  );
}

else {
$form['venue_select']['venue_node'] = array(
    '#type' => 'select',
    '#options' => _concert_content_type_venue_options_list(),
    '#title' => t('venue node select'),
    '#default_value' => '466', // node id is select value
  );
if (!empty($form_state['values']['venue_node']))
$form['concert_venue_node']['und']['#default_value'] = $form_state['values']['venue_node']; 
}

function concert_venue_callback($form, $form_state) {
   return $form['venue_select'];
}

The options from the selectbox are populated by a function in a file accessable for both the .install and the .module file:

function _concert_content_type_venue_options_list() {

$results = db_select('node', 'n')
 ->fields('n', array('nid', 'title'))
 ->condition('n.status', 1)
 ->condition('n.type', 'venue')
 ->execute();

$options = array();
  foreach ($results as $node) {
    $options[$node->nid] = $node->title;
  }
return $options;
}

1 Answer 1

1

Setting #default_value for the 'venue_radio' form element solved my problem: http://drupal.org/node/1545316#comment-5914060 I also changed my code to how it is described in the Ajax altering node form example. Everything is working now in the way I wanted it to. Issue solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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