Big Bob
Big Bob

Reputation: 43

Multiple Forms in Drupal 7

I wish to add a "todo" list to my site so that a logged in user can maintain a simple list of tasks to do. Conceptually I want to display an array of input boxes, allowing the user to edit any of the existing tasks, add a new task, or delete an existing task. Each input box will be its own form so that changes can be submitted one-by-one. I'm completely new to drupal and can't seem to find any resource online that can show how to achieve this.

Upvotes: 3

Views: 5266

Answers (2)

chx
chx

Reputation: 11760

You need to write a page callback which calls drupal_get_form several times. If the same form builder handles the forms, then you need to implement hook_forms.

function foo_menu() {
  $items['foo'] = array(
    'page callback' => 'foo_page',
    'access arguments' => array('access foo'),
  );
  return $items;
}
function foo_page() {
  for ($i = 0; $i < 10; $i++) {
    $build[] = drupal_get_form('foo_form_' . $i, $i);
  }
  return $build;
}
function foo_forms($form_id, $args) {
  if (!empty($args) && $form_id == 'foo_form_' . $args[0]) {
    $forms[$form_id]['callback'] = 'foo_form';
  }
  return $forms;
}
function foo_form($form, $form_state, $i) {
  return $form;
}

Of course, if the forms are different then omit foo_forms and just write foo_form_0, foo_form_1 etc etc.

Upvotes: 7

mbrakken
mbrakken

Reputation: 96

Alternatively, you could user the myTinyTodo module (http://drupal.org/project/mytinytodo) which implements http://www.mytinytodo.net/. I'm using it on a site and it's flexible, ajaxified, allows prioritization and annotation of items, and other cool stuff.

Upvotes: 1

Related Questions