1

Sorry if this question has been answered already but I couldn't find an exact answer. Maybe I'm missing something.

Context of my issue:

I have a view that lists fields of certain content type. One of the fields is the nid. I need to do some validation in the view template before printing anything. So I need to call a function and check something using the nid as parameter. In my views-view-fields.tpl.php I do something like:

<?php foreach ($fields as $id => $field): ?>
    <?php if ($id == "nid"): ?>  
        <?php my_function_here($field->content); ?>
    <?php endif; ?>
<?php endforeach; ?>

As you might expect, $field->content is a string and if I do something like this in my function, it just won't work.

function my_function_here($nid) {
    var_dump(node_load($nid));
}

I tried casting the value to integer, but still no luck (casting returns 0):

function my_function_here($nid) {
    $nid = (int)$nid;
    var_dump(node_load($nid));
}

and

function my_function_here($nid) {
    $nid = intval($nid);
    var_dump(node_load($nid));
}

So my question is: how do I get the nid in that view template?

Big thanks in advance!

2

From a similar issue - you can find details here (or just search for views preprocessor):

A preprocessor will give you access to the data, instead of rendered html which you find in the tpl file.

There aren't dynamically generated preprocess functions; instead, all of the theme functions use the base preprocessor automatically. So views-view--my-first-view.tpl.php still uses template_preprocess_views_view(&$vars) as its preprocessor function.

You can add preprocess functions for specific views with the exact same naming conventions. Note that that it's named *preprocess not preprocessor_.

Note that currently Drupal's design requires that you have the .tpl.php file in order to have the preprocess function (though you could drop in a generic preprocess function and do switches based upon information contained within the $vars['view'] variable. There's an issue for this which is fairly easily found by going to the Drupal queue and searching in the 'theme system' queue.

| improve this answer | |

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.