Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save randallknutson/5049328 to your computer and use it in GitHub Desktop.
Save randallknutson/5049328 to your computer and use it in GitHub Desktop.
Redirect after registration to node page
/**
* Implements hook_file_download()
*/
function mymodule_file_download($uri) {
$url = drupal_parse_url($_SERVER['HTTP_REFERER']);
// Strip out the server name.
$path = substr($url['path'], strpos($url['path'], $_SERVER['SERVER_NAME']) + strlen($_SERVER['SERVER_NAME']) + 1);
if ($path == 'user/login') {
// If we get here the user just logged or registered. If we simply
// return the file they will stay on the login page. We need to find the
// node that contains the file and send them back to that with a message.
$existing_files = file_load_multiple(array(), array('uri' => $uri));
if (count($existing_files)) {
$existing = reset($existing_files);
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->fieldcondition('field_private_file', 'fid', $existing->fid); // Make sure the field name matches the field created in step 3
$result = $query->execute();
if (!empty($result)) {
$nid = reset(array_keys($result['node']));
drupal_set_message('You have logged in and can now download files. Please select the file again below.');
drupal_goto('node/' . $nid);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment