Multiple forms in Drupal 7

I want to add a todo list to my site so that a registered user can maintain a simple todo list. Conceptually, I want to display an array of input fields, allowing the user to edit any of the existing tasks, add a new task, or delete an existing task. Each input field will be its own form, so changes can be presented one after another. I am completely unfamiliar with drupal and cannot find any resource on the Internet that can show how to achieve this.

+3
source share
2 answers

You need to write a page callback that calls drupal_get_formseveral times. If the same form editor processes forms, then you need to implement it 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_formsand just write foo_form_0, foo_form_1etc. etc.

+7
source

Alternatively, you can use the myTinyTodo module ( http://drupal.org/project/mytinytodo ), which implements http://www.mytinytodo.net/ . I use it on the site and it is flexible, ajaxified, allows you to prioritize and annotate elements and other interesting things.

+1
source

All Articles