1

I have to create a website that allows for a "new" tag to be floated above certain pictures that have the specific taxonomy term of "new". I created a view for the "new" where I added an empty div below the image and then floated it above the image and added the actual "new" image with CSS. This is what it looks like For the "new" node.tpl.php the code looks like this:

<article class="node-<?php print $node->nid; ?> 
<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
<div id="new-intro"><?php
// We hide the comments and links now so that we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
?>

<?php print render($content['links']); ?></div>

<?php print render($content['comments']); ?>

</article>

The id="new-intro" is the code that adds the background image to the empty div and places it above the product image. So what I need is a code that automatically adds the id="new-intro" based on whether the product has the taxonomy term of "new" I've found quite a few PHP samples but my knowledge is fairly limited and so I'm not sure that I'm editing them properly. Can any one point me in the right direction?? Thanks so much!!

Edit: So using the code below my code looks like this: template.php:

function hayfield_preprocess_node(&$vars) {
  $node = $vars['node'];
  $vars['new_introduction'] = FALSE;
  if ($terms = field_get_items('node', $node, $field_new_introduction)) {
    foreach ($terms as $term) {
      if ($term['tid'] == 'new') {
        $vars['new_introduction'] = t('!published' == TRUE');
      }
    }
  }
}

and the node code looks like this:

<article class="node-<?php print $node->nid; ?> <?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <div <?php if ($new_introduction): ?> <?php print 'class="new-intro"' ?> <?php endif; ?>><?php
    // We hide the comments and links now so that we can render them later.
    hide($content['comments']);
    hide($content['links']);
    print render($content);
  ?>

  <?php print render($content['links']); ?></div>

  <?php print render($content['comments']); ?>

</article>

But it's throwing major errors :

Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /Users/Candace Morgan 2/Sites/hayfield/sites/all/themes/hayfield/template.php on line 33
(it doesn't like my argument in the last line of the template.php)

Fatal error: Cannot access empty property in /Users/Candace Morgan 2/Sites/hayfield/modules/field/field.module on line 936

the Content type is product_display, the field in the content type is field_new_introduction, the taxonomy vocabulary name is new and the taxonomy term name I want to reference is New Introduction.

I'm yanking my hair out on this one!! Thank you for any help you can give me!!

  • As side note, t('!published' == TRUE'); should probably be t('!published == TRUE');. Even in that case, there is no need to pass to t() a string you are using just for debugging, as it will never translate, for the fact nobody is providing a translation for it that is available on http://localize.drupal.org. – kiamlaluno Oct 25 '12 at 8:26
0

I almost fell out of my chair when I took the preprocessor code out and changed my node code and it worked!!! (at least so far so good!!)

<article class="node-<?php print $node->nid; ?> <?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <div <?php if ($field_new_introduction): ?> <?php print 'class="new-intro"' ?> <?php endif; ?>><?php
    // We hide the comments and links now so that we can render them later.
    hide($content['comments']);
    hide($content['links']);
    print render($content);
  ?>

  <?php print render($content['links']); ?></div>

  <?php print render($content['comments']); ?>

</article>

I just made sure that the field for new introduction field had only one option so that it's either selected or not and I think that's going to do it! (I hope!!) Thanks for your help!!!!

0

Put this into your themes:

function THEMENAME_preprocess_node(&$vars) {
  $node = $vars['node'];
  $vars['new'] = FALSE;
  if ($terms = field_get_items('node', $node, $FIELD_NAME_OF_YOUR_TERM_FIELD_IN_NODE)) {
    foreach ($terms as $term) {
      if ($term['tid'] == THE_TERM_ID_OF_THE_NEW_TERM) {
        $vars['new'] = t('Maybe you can add something new text or simple set it TRUE');
      }
    }
  }
}

Then empty the theme_registry (clear caches) and change the <div id="new-intro"> to:

  <?php if ($new): ?>
    <span class="new-intro"><?php print $new ?></span>
  <?php endif; ?>

Using the same ID in one page is not valid. Then you can target using CSS styles.

  • Thanks for reminding me about the ID!! (Duh!!) So if my taxonomy vocabulary name is "new" and the term is "New Introduction" I've edited your code as follows: function zen_prepocess_node(&$vars) { $node = $vars['node']; $vars['new'] = FALSE; if ($terms = field_get_items('node', $node, $new)) { foreach ($terms as $term) { if ($term['tid'] == new_introduction) { $vars['new'] = t('!submitted' == TRUE); } } } } I'm not quite what sure I should put in the last argument or if this makes sense. – everaftergraphics Oct 3 '12 at 14:08
  • and for the node.tpl.php: <div><?php if ($new): ?> <span class="new-intro"><?php print $new ?></span> <?php endif; ?> I'm trying to get this working but I'm obviously still doing something wrong because it's returning I have an "undefined variable". I really appreciate your patience and help with me!! Thanks!!! – everaftergraphics Oct 3 '12 at 14:10
  • 2 things: zen_preprocess_node (missing r in function ) and what do you do in zen? This is a base theme, you shouldn't modify it. See, how to create a subtheme: drupal.org/node/873778 – Gabor Szanto Oct 3 '12 at 15:42
  • Thank you Gabor!! I feel like a toddler that thinks she can run and then falls flat on her face... I don't know what I was doing in Zen - head slap I'll fix those issues and hopefully it will start working! – everaftergraphics Oct 3 '12 at 22:59
  • I'm pulling my hair out on this one... it keeps saying that I have an undefined variable in the node. So $new is undefined and I can't figure out why! I'll re-post the code I'm using... – everaftergraphics Oct 4 '12 at 12:58

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.