0

I'm using drupal 7. I have a content type called News Item. On my home page, I have a block+view that lists the most recent news items. What is shown is the title of the news item, a brief description, and a read more link to the news item. When a person clicks on the read more link, it takes them to a page that displays the entire news item.

I want the ability to specify a redirect on some news items. If a news item has a redirect, whenever a person clicks on the read more link in the home page, they will be redirected to a different url, NOT the url of the actual news item. And if a person types in the url of the actual news item, they will be redirected to the different url instead.

How do I implement this kind of functionality?

1

I think you could handle this in a views row level template file.

First set up a field for the redirect which is optional. Then add it to your view. Create the appropriate views row template file. In your template file print out the fields individually adding a bit of logic around the read more link:

<?php if ($fields['news_url']): ?>
  <?php print $fields['news_url']; ?>
<?php else: ?>
  <?php print $fields['readmore']; ?>
<?php endif; ?>

Obviously the field names are wrong but hopefully this gives you the idea.

0

I added a field called url_redirect to the relevant content types. Then in my theme_process_html() function of template.php, i added the following code:

if(isset($variables['node']->field_url_redirect))
{
  if(is_array($variables['node']->field_url_redirect))
  {
    $temp = current($variables['node']->field_url_redirect);
    if(is_array($temp))
    {
      $temp = current($temp);
      if(is_array($temp))
      {
        $url_redirect = $temp['value'];
        if(preg_match('/^\w[\w\d_-]*\./',$url_redirect))
        {
          $url_redirect = 'http://'.$url_redirect;
        }
        else
        {
          $url_redirect = '/'.$url_redirect;
        }
        if(isset($variables['logged_in']))
        {
          $url_redirect = '/redirect?node='.$variables['node']->nid.'&rurl='.urlencode($url_redirect);
          if(isset($variables['node']->current_version_id))
          $url_redirect .= '&v='.$variables['node']->current_version_id;
        }
        header('Location: '.$url_redirect);
        exit;
      }
    }
  }
}

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.