Drupal views_embed_view() Returns NULL Result

File this one under "Stupid Mistakes".

I recently spent an obscene amount of time troubleshooting one of my bone-headed mistakes. Views 2 has a function called function views_embed_view($name, $display_id). This function accepts the arguments that are to be passed into Views for refining the result set. When using it this time I kept getting a NULL result, which was driving me nuts and totally inexplicable. I'd stared at the code for so long, and read so many blog entries and forum posts I ended up looking right past the problem. It took opening the Views source & actually reading through the function to locate my error (ummm... oops!). I was using something like:

views_embed_view("content_list", "block_1", Array($arg1, $arg2, $arg3));

Some of you already see the problem. Somewhere I'd gotten the impression that there were three arguments, but there are not, the third argument to the function is not an array of Views arguments, but rather all views arguments should be just added on to the views_embed_view function arguments list, like so:

views_embed_view("content_list", "block_1", $arg1, $arg2, $arg3);

Most people reading the Views documentation or copying an example will not have this problem, but on the off change that someone else is looking (or I make the same mistake in the future), the fix is simply to not pass the Views arguments inside of an array.

The more interesting part about this experiencing is in reinforcing the idea that the best documentation you can read is the source code you're working with. I spent a lot of time searching the web, but all of about 20 seconds looking at the actual source before seeing the problem. Being more familiar with the source I'm working with would have saved me tons of time. Next time...

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.