You are here

function redirect_save in Redirect 7.2

Same name and namespace in other branches
  1. 7 redirect.module \redirect_save()

Save an URL redirect.

Parameters

$redirect: The URL redirect object to be saved. If $redirect->rid is omitted (or $redirect->is_new is TRUE), a new redirect will be added.

Related topics

9 calls to redirect_save()
drush_redirect_create_redirect in ./redirect.drush.inc
Command callback. Validates and adds a redirect.
MigrateRedirectEntityHandler::complete in ./redirect.migrate.inc
Overrides complete().
RedirectFunctionalTest::testPageCache in ./redirect.test
RedirectTestHelper::addRedirect in ./redirect.test
Add an URL redirection
redirect_change_status_multiple in ./redirect.module
Change the status of multiple URL redirects.

... See full list

File

./redirect.module, line 808

Code

function redirect_save($redirect) {
  $transaction = db_transaction();
  try {
    if (!empty($redirect->rid) && !isset($redirect->original)) {
      $redirect->original = entity_load_unchanged('redirect', $redirect->rid);
    }

    // Determine if we will be inserting a new node.
    if (!isset($redirect->is_new)) {
      $redirect->is_new = empty($redirect->rid);
    }

    // The changed timestamp is always updated for bookkeeping purposes.

    //$redirect->changed = time();
    redirect_hash($redirect);
    if ($redirect->is_new || $redirect->hash != $redirect->original->hash) {

      // Only new or changed redirects reset the last used value.
      $redirect->count = 0;
      $redirect->access = 0;
    }

    // Allow other modules to alter the redirect before saving.
    module_invoke_all('redirect_presave', $redirect);
    module_invoke_all('entity_presave', $redirect, 'redirect');

    // If a duplicate redirect exists, we need to update it, rather than save a
    // new one because the UUID hash will be the same. This will produce an
    // integrity constraint violation in MySQL.
    if ($exists = redirect_load_by_hash($redirect->hash)) {
      $redirect->rid = $exists->rid;
      $redirect->is_new = FALSE;
    }

    // Save the redirect to the database and invoke the post-save hooks.
    if ($redirect->is_new) {
      drupal_write_record('redirect', $redirect);
      module_invoke_all('redirect_insert', $redirect);
      module_invoke_all('entity_insert', $redirect, 'redirect');
    }
    else {
      drupal_write_record('redirect', $redirect, array(
        'rid',
      ));
      module_invoke_all('redirect_update', $redirect);
      module_invoke_all('entity_update', $redirect, 'redirect');
    }

    // Clear internal properties.
    unset($redirect->is_new);
    unset($redirect->original);

    // Clear the static loading cache.
    entity_get_controller('redirect')
      ->resetCache(array(
      $redirect->rid,
    ));

    // Ignore slave server temporarily to give time for the
    // saved node to be propagated to the slave.
    db_ignore_slave();
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog_exception('redirect', $e);
    throw $e;
  }
}