You are here

function captcha_update_7000 in CAPTCHA 7

Migrate form configuration for changed form ids in Drupal 7.

File

./captcha.install, line 285
Install, update and uninstall functions for the CAPTCHA module.

Code

function captcha_update_7000() {

  // 'user_register' became 'user_register_form'.
  db_update('captcha_points')
    ->fields(array(
    'form_id' => 'user_register_form',
  ))
    ->condition('form_id', 'user_register')
    ->execute();

  // 'contact_mail_page' became 'contact_site_form'.
  db_update('captcha_points')
    ->fields(array(
    'form_id' => 'contact_site_form',
  ))
    ->condition('form_id', 'contact_mail_page')
    ->execute();

  // 'contact_mail_user' became 'contact_personal_form'.
  db_update('captcha_points')
    ->fields(array(
    'form_id' => 'contact_personal_form',
  ))
    ->condition('form_id', 'contact_mail_user')
    ->execute();

  // The D6-style comment_form form_id is split per node type
  // in D7: comment_node_{type}_form, e.g. comment_node_page_form.
  // Get the current settings for 'comment_form'.
  $captcha_point = db_query("SELECT * FROM {captcha_points} WHERE form_id = :comment_form_id", array(
    ':comment_form_id' => 'comment_form',
  ))
    ->fetchObject();
  if ($captcha_point !== FALSE) {

    // Create entries for D7-style node form IDs.
    $module = $captcha_point->module;
    $captcha_type = $captcha_point->captcha_type;
    foreach (node_type_get_names() as $type => $name) {
      $form_id = 'comment_node_' . $type . '_form';
      db_insert('captcha_points')
        ->fields(array(
        'form_id' => $form_id,
        'module' => $module,
        'captcha_type' => $captcha_type,
      ))
        ->execute();
    }

    // Delete outdated entry.
    db_delete('captcha_points')
      ->condition('form_id', 'comment_form')
      ->execute();
  }
}