1

I have a content type defined with a field storing the URL for accessing an external web resource and am looking for a way to have my Drupal 7 instance manage the redirection to this external site based on the nid contained in the incoming request path (ie. mysite.com/resource/123 redirects to the location stored in field_data_field_url where nid = 123).

The driver for this requirement is that our partner sites need to publish links to these resources that will not be subject to change. Drupal will house the database of resources and I am hoping to provide an endpoint that handles the redirect without further action from the user.

I have begun exploring the Services module in attempting to solve this problem but am not making headway in figuring out how to ultimately perform a redirection. I would appreciate input on whether this is an appropriate case for this web service approach or should I be looking at another way to tackle this problem within the Drupal framework?

  • Not sure if it needs to be that complicated. The standard permalink URL of a node is always node/nid, that will never change. And a node can have an alias, manually managed or auto-generated with Pathauto. The global redirect (drupal.org/project/globalredirect) module automatically makes sure that request to node/nid are automatically redirect to the current alias. What's the difference to your system? – Berdir Jul 24 '12 at 18:35
  • @Berdir we maintain a database of external e-resources and the links for accessing the resources are subject to change. I needed a way to publish a static link for use by our partner sites to avoid having to manage the updating of links on each partner site every time the vendors changed their URLs. I give them a node/nid link on my site and execute a database lookup to determine where to send them for the external resource and then take them there. See code below. – WuWei Jul 24 '12 at 19:16
0

You could use Services, you dont have to.

1) To have remote resources you need to create a url in drupal to consume data sent to it (Services can indeed do this). eg, mysite.com/secret/submit-a-resource-location. You could make this in a custom module with hook_menu, it could store values in a table with NID, and RESOURCE_URL as the columns.

2) If you goto some path on your site like mysite.com/passthrough/NID you could look at the NID passed in and in the hook_menu page_callback function and use drupal_goto to go to your remote url.

Eg:

function mymodule_menu() {
  $items['secret/submit-a-resource-location'] = array ( /* ... */ );
  $items['passthrough/%'] = array( /* ... */ );
  return $items;
}

function mymodule_passthrough_page_content($arg) {
  $result = db_query("SELECT resource_url FROM {external_resources} er WHERE er.nid = :nid", array(':nid' => $arg))->fetchCol();
  drupal_goto($result['resource_url'], 302);
  // implicit exit here by drupal goto.
}
  • this approach seems to work but I am having trouble figuring out how to populate $arg with the NID path component. Looking at the examples in the hook_menu() documentation to see what is possibly missing, I tried modifying the callback function paramaters with ($arg = 0) and it still doesn't work. I tested the function with a hardcoded nid value and it works. Any suggestions? Thanks for getting me on track with this! – WuWei Jul 24 '12 at 16:24
  • The problem appears to be with the wildcard in the path. After trying multiple combinations, I have it working with the menu item defined as $items['api/redirect']. Will post full working code shortly. – WuWei Jul 24 '12 at 17:46
0

Here is the working code for those interested (accessed via mysite.com/api/redirect/123):

/**
* Implements hook_menu()
*/
function resource_redirect_menu() {
  $items['api/redirect'] = array(
    'page callback' => 'resource_redirect',
    'access arguments' => array('access content'),
  );
  return $items;
}

function resource_redirect($arg = 0) {
  $result = db_query("SELECT field_url_url FROM field_data_field_url fd WHERE fd.entity_id = :nid", array(':nid' => $arg))->fetchCol();
  drupal_goto($result[0]);  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.