1

I have a view that I use to grab a related node field given a user ID. I only call this view from code. The problem is, I just can't get it to work for a certain case.

To start with, here's the code that I use to call the view. The view accepts one argument (the user ID) and displays one field (the related node field). (I use multiple displays in this view for different code functions, thus why I build 'block_3'. Also, I'm calling this code from within a "User has logged in" Rule, which is why I use $account instead of global $user.)

$view = views_get_view('my_view_name');
$view->set_arguments( $account->uid ); 
$view->build('block_3');
$view->execute();

For the user ID "5", this works perfectly. But for the user ID "14", it returns no results. The frustrating part is that in the View settings, I can enter "14" for the preview argument and it ALSO works perfectly!

I'm at my wit's end. Why would the view work perfectly fine in the preview and for one user ID, but not for another?

Some notes

I've confirmed with dpm() that $account->uid is being set properly. I've also changed User ID 14's roles to match User ID 5's, and even swapped the related nodes for the two accounts, but User ID 14 always fails to work while User ID 5 has no issues.
If I pass $view->result to dpm(), I get empty text; dpm($view) returns the $view object, where I can see that $view->result is an empty text.

0

2 Answers 2

2

The View set_arguments() method takes an array as it's first parameter; currently you're passing a string/int.

This code will work.

$view->set_arguments(array($account->uid));

I think you're right about the C-style access to a string-as-an-array-of-chars in PHP. The following code prints the letter e, which would certainly support that theory.

$x = 'test';
print $x[1];

I must say I'm surprised that Views doesn't check the validity of the parameter passed to it, but hey ho!

0

Don't you need to print the output, or return it in an output variable?

I've used something to this before.

print views_embed_view($name, $display_id);

Use print in a template file; use $output = in a module.

View arguments can follow the display ID.

1
  • Sorry, I neglected the output code because I just use the generated result as another php variable. Here's what it looks like. $node_field_value = $view->result[0]->node_data_field_user_account_field_person_id_value;
    – Goluxas
    Commented Apr 30, 2012 at 16:06

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.