0

If I use the following code in template.php and print $link in node--post-esl-material.tpl.php, everything works fine; I get "hey baby."

function eslmooc_preprocess_node(&$vars, $hook) {
   if ($vars['type'] == 'post_esl_material'){
      $vars['link'] ='hey baby';
   }
}

However, if I use the following code and print $link in node--post-esl-material.tpl.php, it doesn't print anything.

function eslmooc_preprocess_node(&$vars, $hook) {
   if ($vars['type'] == 'post_esl_material'){
      $xyz = '';
      if (empty($content['field_link']))
         $xyz .= '';    
      else {
         $xyz .= '<div class="link">';
          $xyz .= '<h4>LINK</h4>';
           $xyz .= '<div class="field_link">';
            $xyz .= $content['field_link']; 
           $xyz .= '</div>';
         $xyz .= '</div>';
      }
      $vars['link'] = $xyz;
   }
}

I cannot get the PHP logic to work. Can anyone see a mistake within the code?

1
  • If that condition returns true, then it will be empty. Just check that. Commented Jul 6, 2013 at 11:23

2 Answers 2

2

I think that the problem is that $content is not defined there. You have it only inside the template file but not in the preprocess hook.

0
0

The following code works:

function eslmooc_preprocess_node(&$vars, $hook) {
   if ($vars['type'] == 'post_esl_material'){
      $xyz = '';
      $node = $vars['node'];
      $field_link = field_view_field('node', $node, 'field_link');

      if (empty($field_link))
         $xyz .= '';    
      else {
     $xyz .= '<div class="link">';
       $xyz .= '<h4>LINK</h4>';
         $xyz .= '<div class="field_link">';
           $xyz .= drupal_render($field_link);
         $xyz .= '</div>';
     $xyz .= '</div>';
      }

      $vars['link'] = $xyz;
   }
}
1
  • field_view_value() can be used to get only the value.
    – Elin Y.
    Commented Jul 6, 2013 at 18:56

Your Answer

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

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