0

This is a follow up question to: How do I show user attributes on articles?

I still can't get the "Twitter URL" field to show up on articles, and got tired of reading through the Fields API docs; I figured I couldd ask if anyone can look at my template files, and tell me why the twitter URL is not output, as I know it is being set.

This is the node preprocess function from node.tpl.php:

function blogbuzz_preprocess_node(&$vars, &$variables) {
  if ($vars['view_mode'] != 'full' && $vars['id'] == 1) {
    $vars['classes_array'][] = 'node-first';
  }

  if ($vars['view_mode'] == 'full' && node_is_page($vars['node'])) {
    $vars['classes_array'][] = 'node-full';

  }
  // Even though there's a user object in $variables['user'], we need to
  // load a new copy because the preloaded one doesn't have have field data
  $user = user_load($variables['uid']);

  // Retrieve the data and its value for the Twitter URL field
  // Replace field_twitter_url with the short name of the field
  // found at admin/config/people/accounts/fields
  $items = field_get_items('user', $user, 'field_twitter_url');
  $field = field_view_value('user', $user, 'field_twitter_url', $items[0]);

  // Get the rendered output of the field for use in the submitted line
  $twitter_url = drupal_render($field);

  // If you wanted to get more fields, repeat the last three lines, replacing
  // field_twitter_url with the names of the other fields.
  $variables['twitter_url'] = $twitter_url;
}

And here is the template.tpl where I use $twitter_url:

<div class="<?php print $classes; ?>">
  <div class="node-inner clearfix">
    <?php print render($title_prefix); ?>
    <?php if (!$page): ?>
      <h2<?php print $title_attributes; ?>><a href="<?php print $node_url; ?>"><?php print $title; ?></a></h2>
    <?php endif; ?>
    <?php print render($title_suffix); ?>

    <div class="meta">
      <?php if ($display_submitted): ?>
        <?php print t('Published by '); ?><?php print $name.t(' on ').$date; ?>
         <?php print t('Twitter '); ?><?php print $twitter_url; ?>
      <?php endif; ?>

      <?php if (!empty($content['field_tags'])): ?>
        <?php print ' in '.render($content['field_tags']);  ?>
      <?php endif; ?>

    </div>
    <div class="content clearfix">
    <?php
      hide($content['comments']);
      hide($content['links']);
      hide($content['field_tags']);
      print render($content);
    ?>
    </div>

    <?php if ($content['links']): ?>
      <div class="extra-links">
        <?php if (!$page): ?>
          <div class="read-more"><?php print l(t('Read more'), 'node/' . $nid, array('attributes' => array('class' => t('node-readmore-link')))); ?></div>
        <?php endif; ?>
        <?php print render($content['links']); ?>
      </div>
    <?php endif; ?>

    <?php print render($content['comments']); ?>
  </div>
</div> <!-- /node-inner, /node -->

1 Answer 1

1

You are trying to render the twitter_url twice. First in then preprocess function and then again in the template.

If that's not the problem, start debugging. Install devel.module if you haven't already and add dpm($variable) statements on the relevant places, e.g. dpm($twitter_url) in the preprocess function.

4
  • is there a module that could take care of this?
    – Mike
    Commented Dec 29, 2011 at 20:29
  • take care of what? You can't tell a module to debug your code. But there is the mentioned devel module that provides functions to assist you.
    – Berdir
    Commented Dec 29, 2011 at 21:04
  • sorry, i should have specified. Is there a module to take care of showing fields I've added to users under the nodes that they make?
    – Mike
    Commented Dec 30, 2011 at 18:51
  • No, I don't think so. That's something that would be hard to configure, how and where should they be displayed and so on...
    – Berdir
    Commented Dec 30, 2011 at 20:48

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.