0

I've read several custom module tutorials but none seem to show how to edit a record. My module does not use node but has a custom table. So, I think I ought to be able to drupal_get_form and populate the #value with the fields. But to set up the menu to get the parameter in the URL and my page callback is not working.

Looking around I found this answer and modeled the below after it but I get a default page instead of my expected form.

$items['redashboard/project/%/edit'] = array(
  'title' => (t('Edit project')),
  'page callback' => 'drupal_get_form',
  'page arguments' => array('re_dashboard_edit_project_form', 2),
  'access callback' => TRUE,
);

function re_dashboard_edit_project_form($form, $form_state, $projectid) {
  $project = pull_project($projectid);
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Research Project Name/Question'),
    '#value' => $project->projectName,
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Create',
  );
  return $form;
}

function pull_project($projectid) {
  $query = db_select('Projects', 'p');
  $query->fields('p', array('ProjectID', 'residentID', 'projectName', 'projectMentor', 'projectStatus', 'citation', 'lastUpdated'));
  $query->condition('ProjectID', $projectid);
  $result = $query->execute();
  return $result->fetch();
}

So, how should I set up a page to edit this record if not this way? Or to the point how should I set up the menu so that this callback gets rendered? thx, sam

1 Answer 1

1

The $items['redashboard/project/%/edit'] declaration needs to be made inside of re_dashboard_menu(), and there needs to be a re_dashboard_edit_project_form_submit() function to pipe $form_state['values'] into db_update().

Other than that, it looks like it will get the job done. You could use db_query() instead of db_select(), since it's faster, and the query conditions are static. Also, make sure you have a way to handle db_select() not returning any results, since fetch() will return FALSE in that case.

2
  • Yes, the menu() and re_dashboard_edit_project_form_submit() was left off for clarity. But you are right and will check for empty in my submit handler. But I'm still not getting to this page, even after drush cc all
    – sam452
    Jun 18, 2015 at 23:52
  • Another day, a fresh restart seems to have shaken its cobwebs. Thanks for validating the page args.
    – sam452
    Jun 19, 2015 at 20:02

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.