By Mauriel on
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
use #ajax
You can do this using drupal form ajax api(#ajax)
chetan
Thanks!
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
A suggestion
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:)
Thanks, I'll definitely take
Thanks, I'll definitely take a look into that.
Glad to have helped
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).