Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

My logic demands that for a specific content type provided a node (Ex) node/123 has one if its CCK field named web link with a URL value like http://someothersite.com in it, viewing the given node mysite/node/123 should redirect to the respective URL http://someothersite.com given in that field.

I've implemented this with a custom module's hook_nodeapi view case to check the value of the field and perform the redirect. However doing so has inadvertently affected my apachesolr search indexing which invokes the hook on view condition (through node_build_content) to generate the node for being indexed.

This has forced me to currently skip such specific nodes from not being indexed, however I wish to know if there's a better way of programatically implementing the aforementioned redirect without hook_nodeapi to keep the sanity of search indexing intact.

share|improve this question

1 Answer

up vote 1 down vote accepted

How about doing this in hook_init() instead, eg, something along the lines of...

function YOURMODULE_hook_init() {

   $mgo=menu_get_object();

   if (!empty($mgo->type) && $mgo->type=='THE_TYPE_YOU_ARE_LOOKING_FOR') {

      if (!arg(2)) { // eg so we ignore node/###/edit etc
        if (!empty($mgo->field_THE_FIELD_YOU_ARE_REDIRECTING_TO[0]['value'])) {
          drupal_goto($mgo->field_THE_FIELD_YOU_ARE_REDIRECTING_TO[0]['value']);
        }
      }
   }

}

Caveat: I've never used apachesolr, so you may need to wrap this with further logic to make sure you are running when a user actually goes to the page, eg, arg(0)=='node' and is_numeric(arg(1)) kinda stuff.

ADDITION:

You might be able to just check args inside nodeapi() as well, eg:

if (arg(0)=='node' && is_numeric(arg(1)) && !arg(2)) { 
  // do your magic
}
share|improve this answer
Thanks for the elaborate and well thought response, I was shying to use hook_init and was curious to know if there was some hook that would be better suited or tailor made for the occasion that I might have missed googling or finding from the API.. – optimusprime619 May 6 at 21:20
Thanks again, worked like a charm! – optimusprime619 May 7 at 16:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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