0

I'm new to Drupal and have inherited a fully functional system (Drupal 7.43), I've had a look on Google with no joy, so hopefully you can help.

For a specific content type it has the following fields (simplified)

URL info1 info2

When the user clicks to read the page if info1 is empty I would like to redirect to the URL (which is an external, absolute URL) if there is content in info1 I would like to show the page as normal.

How would I go about doing this?

1 Answer 1

1

No problem bro, its pretty simple. I'll write you a step by step tutorial as an edit. Standby.

You will need to create a preprocess function. Add a folder and a file in your theme. THEMENAME/preprocess/node.preprocess.inc with this code:

<?php

/**
 * Implements hook_preprocess_node().
 *
 * Hyjacking the nodes normal node link if an external link is specified
 *
 */
function YOURTHEMENAME_preprocess_node(&$variables) {

  $url = NULL;

  switch ($variables['type']) {
    case 'article':
      if (isset($variables['alternate_link']['und'][0]['url'])) {
        $url = (!empty($variables['alternate_link']['und'][0]['url'])) ? $variables['alternate_link']['und'][0]['url'] : NULL;
        $variables['node_url'] = $url;
      }
    break;
  }

}

My example states that In the case of a Content Type with the machine name of 'article' (yours may be different) do something.

THEN check if my field with the machine name of 'alternative_link' (your machine name will be different) has a value then do something.

It will then use the node's default $url variable.

If it is not set, it will ignore this code and leave your $url clean

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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