Hi, I'm very new to Drupal and web development.

I have an entity form with a Date field as a popup calendar and a list(text). I want to alter the list (add options from an external database) when the selected date is changed, so I wanna add a hook to my module to achieve this.

My question is: what hook responds to such event?
I hope I made sense, any help is sincerely appreciated.

Edit: Thanks everybody for your help. I solved this by using the ajax api in a hook_form_alter as you suggested in the comments. My code looks like this:

function db_connect_form_alter(&$form, &$form_state, $form_id){
if($form_id == "create_new_deployment_entityform_edit_form"){
$form['field_field_activity']['#prefix'] = '<div id="activities-div">';
    $form['field_field_activity']['#suffix'] = '</div>';
    if (isset($form_state['values']['field_sites']['und'][0])){
      $form['field_field_activity'] = array(
      '#type' => 'select',
      '#title' => t('Field Activities'),
      '#options' => $form_state['values']['field_sites']['und'][0],
      // Add the div again since we are creating a new array
      '#prefix' => '<div id="activities-div">',
      '#suffix' => '</div>',
      );
    }

//Sites
    $options = array();
    $result = db_select('sites', 's')->fields('s')->execute();
    for ($index = 0; $index < $result->rowCount(); $index++)
    {
      $item = $result->fetchObject();
      $options[$item->SiteID] = $item->SiteName;
    }

    $form['field_sites']['und']['#type'] = 'select';
    $form['field_sites']['und']['#options'] = $options;
    $form['field_sites']['und']['#ajax'] = array(
      'callback' => 'db_connect_list_activities_callback',
      'wrapper' => 'activities-div',
    );
    }
}

function db_connect_list_activities_callback($form, $form_state) {
  return $form['field_field_activity'];
}

Comments

chetan-singhal’s picture

You can do this using drupal form ajax api(#ajax)

chetan

Mauriel’s picture

Thanks, I feel I'm headed in the right direction now. However, I'm stuck at one point. I know that I need my form builder function but the prototypes I've used have not worked, it won't execute. My entity form ID is create_new_deployment and my module is db_connect. Here is my code

/**
 * Implements hook_menu().
 */
function db_connect_menu() {
  $items['create_new_deployment'] = array(
    'title' => 'Example',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('db_connect_create_new_deployment'),
    'access arguments' => array('access content'),
  );
 
  return $items;
}

function db_connect_create_new_deployment($form, &$form_state) {
  // ------- Test Code -------
  print "<pre>";
  print_r($form);
  print "</pre>";
  // -------------------------
  $form['field_sites']['#ajax'] = array(
      'callback' => 'example_callback',
      'wrapper' => 'fields-div',
      'method' => 'replace',
      'effect' => 'fade',
  ); 
  $form['field_field_activity']['und']['#options'] = array(
    'some key' => $form_state['values']['field_sites']
  ); 
  return $form;
}

function example_callback($form, $form_state){
  return $form['field_field_activity'];
}

goofus’s picture

Hello,
To alter an existing form, you implement hook_form_alter https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_form_alter/7. See the link for details, there is another hook that includes the "form id".

If you are new to Drupal development I highly recommend you review the "Examples for Developers" located here: http://drupalcode.org/project/examples.git/tree. You want to review the form examples and the ajax examples.

Pehaps I didn't understand your requirements, but if you are "altering" an existing form, you don't implement hook_menu (establishing a path and a form definition ). You are altering a form definition, you don't create an additional form definition, you implement hook_form_alter (or one of it's variants).

I also highly recommend developing on a local installation with a source code debugger like xdebug. Drupal generate dynamic function calls and data structures. Trying to develop on a remote machine through print statements is really slow and gaurantees a lot of guessing. Hey, but maybe that's just me :)

Hope that helps.
Good Luck:)

Mauriel’s picture

Thanks, I'll definitely take a look into that.

goofus’s picture

Hello,
You are welcome :)
Glad to have helped.

Please remember, once your issue is solved (I know you still have work to do :) ), please edit your original post title and add "[SOLVED]". That way other folks can find the solution (again, when you are ready).