Hi,

I am trying to use the new custom field area and I must say it is very useful. Pardon my ignorance, following question may be weird but:

If I try to use something like print $date.... as in your example video, it is ok. But can I use an array that is defined in my theme's template.php file? When I try something like $terms, it displays nothing. For example:

<div class="taxonomy"><?php print $terms; ?></div>

doesn't work in that area, but if I write it in node.tpl.php, it works...

Thanks,

Sinan

Comments

swentel’s picture

Status: Active » Fixed

The only thing thats available in a custom field is the complete $node object. So things like $terms aren't available there. Now, for terms, you can normally use the taxonomy field which should be available by default in the module - although there is an issue in the queue (from beta 1) that taxonomy doesn't show up - should retest that.

I really need to update the documentation fast enough. You could use something like this though in a custom field:

$terms =  taxonomy_link('taxonomy terms', $node);
print theme('links', $terms, array('class' => 'links inline'));

However, this is executing the same code twice (it's already done in template_preprocess_node and should be available as field)

Sinan Erdem’s picture

Thank you for the quick answer.

I tried and separated the vocabularies with a pre-process function in my template.php file. I used the approach in this article: http://11heavens.com/putting-some-order-in-your-terms

Now, I can use the newly created arrays of variables ($tags, $categories, etc.) in my node.tpl.php file. But not in the custom field. Is it possible to use it somehow?

I am sorry if I am bothering you with this specific issue of my own.

Thanks again for your answer...

Sinan

swentel’s picture

Title: The new custom field » Racing condition in preprocess functions makes $variables not up to date (ie $terms & $links)
Status: Fixed » Active

No problem, we like questions as it makes us understand we make mistakes too :) We identified the problem. We also implement template_preprocess_node in our code, the original $terms variable is moved in our code to $node->content['terms'] .. This works fine as long as there is no template_preprocess_node in template.php because that function runs after our code. So in fact, you found a racing condition problem here. We'll need to rethink the way we deal with variables.

In the meantime, you could use that same code in the custom field, with a few small changes (I applied them already):

  if ($node->taxonomy) {
    // Let's iterate through each term.
    foreach ($node->taxonomy as $term) {
      // We will build a new array where there will be as many
      // nested arrays as there are vocabularies
      // The key for each nested array is the vocabulary ID.     
      $vocabulary[$term->vid]['taxonomy_term_'. $term->tid]  = array(
        'title' => $term->name,
        'href' => taxonomy_term_path($term),
        'attributes' => array(
          'rel' => 'tag', 
          'title' => strip_tags($term->description),
        ),
      );       
    }
    // Making sure vocabularies appear in the same order.
    ksort($vocabulary, SORT_NUMERIC);

    $output = '';
    foreach ($vocabulary as $vid => $terms) {
      // Getting the name of the vocabulary.
      $name = taxonomy_vocabulary_load($vid)->name;
      // Using the theme('links', ...) function to theme terms list.
      $terms = theme('links', $terms, array('class' => 'links inline'));
      // Wrapping the terms list.
      $output .= '<div class="vocabulary taxonomy_vid_';
      $output .= $vid;
      $output .= '">';
      $output .= $name;
      $output .= ':&nbsp;';
      $output .= $terms;
      $output .= '</div>';
    }
    print $output;
  }    

Not so elegant of course, but it should work :)

I reopened the issue and changed the title to reflect the problem, thanks for the report!

Sinan Erdem’s picture

You are fantastic! Thank you for the effort.

[wrong comment deleted by me here not to confuse anybody]

With this fantastic module and generous help, you are making us lazy :)

Cheers,

Sinan

Sinan Erdem’s picture

[wrong comment deleted by me]

I will wait for the next release of the module.

Thanks again..

Sinan Erdem’s picture

Completely ignore my last 3 entries (including this one). It is late and I started to think like a cucumber :)

swentel’s picture

@etcetera9 no worries :) There will be a bit more chatter on this thread by other people also because we're going to brainstorm on how to tackle this problem.

Ok, for the other guys (stalksi, zuuperman, jyve), what are we going to todo here ? We need our preprocess stuff because we need to render data in regions. Currently I see 2 options:

1) Do nothing and tell people, sorry about that. If you want to overcome this problem, well don't implement such stuff and write a lot of documentation on howto make work arounds for it (ie, custom fields UI and custom PHP code). Sounds a bit arrogant, does it ? :)

2) Move the code of our preprocess to a complete separate function. We keep our preprocess_node function which calls this helper function with an extra check however. This check will include some intelligent code (tbd) which detects if people included this a call to this helper function at the bottom in one of the other preprocess_hook functions (engineName_engine_preprocess_hook, engineName_preprocess_hook or themeName_preprocess_hook). This way, we can still keep the code module-centric (and switching themes has no effect) but can be overridable. I'd go for this option.

Thoughts ?

swentel’s picture

Status: Active » Fixed

Ok, went for solution number 2, see http://drupal.org/node/572614 for documentation.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

everest19’s picture

Hi!
How can you have a node and web links displat like newsvine?