Unwanted redirect when using views

fokke - November 16, 2007 - 21:26
Project:CCK Redirection
Version:5.x-1.0
Component:Code
Category:bug report
Priority:critical
Assigned:fokke
Status:reviewed & tested by the community
Description

Somehow the views module triggers hook_nodeapi(&$node, $op = 'view'...) regardless the way a node is viewed as a list, teaser of full page. This leads to an unwanted redirect to the first redirection found in the list.

This can easily be fixed with the attached patch, where the redirection takes place in hook_menu instead.

function cck_redirection_menu($may_cache) {
  if (!$may_cache && arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
    $node = node_load(arg(1));
    $fields = _cck_redirection_get_redirect_fields($node->type);
    if(!empty($fields)) {
      $field = array_shift($fields);
      $data = $node->$field['field_name'];
      $url = check_plain($data[0]['value']);
      if (!empty($url)) {
        if(user_access('bypass redirection')) {
          drupal_set_message(t('This node is redirected to a !r', array('!r' => l(t('remote source'), $url))));
        } else {
          drupal_goto($url);
        }
      }
    }
  }
}

AttachmentSize
cck_redirection.module.patch17.58 KB

#1

fokke - November 16, 2007 - 21:27

By the way, the patch includes more lines than just the changed ones. The checkout I patched returned a file with crappy linebreaks. to me.

#2

moshe weitzman - December 17, 2007 - 18:59

redirecting in hook_menu sounds like a much safer option.

#3

moshe weitzman - December 18, 2007 - 18:11

it would be nice to track down the line that is doing this. i know that token module could do this too.

#4

jmstacey - November 12, 2008 - 22:30

The patch would not apply for me but simply replacing the entire cck_redirection_nodeapi function with the code provided by fokke fixed the problem. I tried creating a patch but ran into the same problems. Either way, it would be nice if this was finally put in an official release since it is a critical problem with views.

#5

jmstacey - November 13, 2008 - 22:17

The file seems to be using CRLF instead of LF line endings (yuck). Here's an uploaded one with LF endings (assuming textmate did it's job). It also has a small fix to check the node_load because the node might not exist which will result in an error later in the code if it runs.

AttachmentSize
cck_redirection.module.zip 2.81 KB
 
 

Drupal is a registered trademark of Dries Buytaert.