0
votes

I have used hook_form_FORM_ID_alter( ) function to alter a menu_edit_menu form to add few fields. I ve stored the values from those fields in a table when submit action is performed. what I need now is when the particular form entry is loaded again for editing. The fields should be populated with the data in the database before form loads. Can anyone tell me how this could be done.

I have tried using hook_validate ( ) function but the function is called when the submit action is performed which is of no use in this regard. I have to perform the database query before the form gets rendered to populate the text fields with data from tables. Kindly provide me some insight into how this could be accomplished.

I have a problem with the sql select query as well/

    $query = db_select('menu_custom');
$query
->fields(array('menu_name','role1','role2','role3'))
->condition('check',1,'=')
->conditon('title',$form_state['values']['title'])
->execute();

foreach($query as $record)
{
    $mname = $result ->menu_name;
    $rl1 = $result -> role1;
    $rl2 = $result -> role2;
    $rl3 = $result -> role3;
    dpm($mname."  ".$rl1);

}

I am getting error that my field specification is wrong but I cant figure out the problem there.

2

2 Answers

1
votes

This is a bit too long for a comment so I'll put it here:

The only error you've got in your query is that the first argument passed to the fields() function needs to be the name/alias of the table from which the fields are coming. So your query should probably look something like this:

$query = db_select('menu_custom')
  ->fields('menu_custom', array('menu_name','role1','role2','role3'))
  ->condition('check',1,'=')
  ->conditon('title',$form_state['values']['title'])
  ->execute();
1
votes

You get the data from your database in your form function, and put them as default_value

$name = db_query(YOUR_SQL);
$form['first_name'] = array(
  '#title' => t('First Name'),
  '#type' => 'textfield',
  '#default_value' => $name,
);

is this what you meant?