You are here

function dbtng_example_form_update in Examples for Developers 7

Sample UI to update a record.

Related topics

1 string reference to 'dbtng_example_form_update'
dbtng_example_menu in dbtng_example/dbtng_example.module
Implements hook_menu().

File

dbtng_example/dbtng_example.module, line 524
This is an example outlining how a module can make use of the new DBTNG database API in Drupal 7.

Code

function dbtng_example_form_update($form, &$form_state) {
  $form = array(
    '#prefix' => '<div id="updateform">',
    '#suffix' => '</div>',
  );
  $entries = dbtng_example_entry_load();
  $keyed_entries = array();
  if (empty($entries)) {
    $form['no_values'] = array(
      '#value' => t("No entries exist in the table dbtng_example table."),
    );
    return $form;
  }
  foreach ($entries as $entry) {
    $options[$entry->pid] = t("@pid: @name @surname (@age)", array(
      '@pid' => $entry->pid,
      '@name' => $entry->name,
      '@surname' => $entry->surname,
      '@age' => $entry->age,
    ));
    $keyed_entries[$entry->pid] = $entry;
  }
  $default_entry = !empty($form_state['values']['pid']) ? $keyed_entries[$form_state['values']['pid']] : $entries[0];
  $form_state['entries'] = $keyed_entries;
  $form['pid'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#title' => t('Choose entry to update'),
    '#default_value' => $default_entry->pid,
    '#ajax' => array(
      'wrapper' => 'updateform',
      'callback' => 'dbtng_example_form_update_callback',
    ),
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Updated first name'),
    '#size' => 15,
    '#default_value' => $default_entry->name,
  );
  $form['surname'] = array(
    '#type' => 'textfield',
    '#title' => t('Updated last name'),
    '#size' => 15,
    '#default_value' => $default_entry->surname,
  );
  $form['age'] = array(
    '#type' => 'textfield',
    '#title' => t('Updated age'),
    '#size' => 4,
    '#default_value' => $default_entry->age,
    '#description' => t("Values greater than 127 will cause an exception"),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );
  return $form;
}