0

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.

1

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
}
1
  • 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 '13 at 21:20

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.