0

I am quite stuck with this, so I hope you can help me out…

I have a view which filters (contextual filter) by 'nid'. I have two 'filter criteria's (view ui):

  • Published
  • Node type

I want to embed that view in a page programmatically through an Ajax call. I have discovered it sometimes returns the correct node and sometimes not. It depends on the specific node so for example it always retrieves the node nid = '4' and it never does for nid = '35'

Trying to figure out where the problem comes from I have tried few things. For example: I have tried to embed the view using panels, just for testing, using arguments for the contextual filter. It always work (always retrieve the correct node) this way. But when I call the view programmatically from a form building function (the one I use inside 'drupal_get_form()'), it just work with some nodes (some values of nid).

I am using devel for debugging and, when the view doesn't returns results,

$view->build_info['fail'] = TRUE

Here is the way I call the view:

$view = views_get_view('proyecto_en_arquitecto');
$view->init(); 
$view->set_display('block_1');
$view->set_arguments('35');
$view->execute();
dpm($view);  //Here is where I can see the building process fails

I am lost as I don't have a clue of why it just works with some nodes…

I hope you can point me to the right direction.

Thanks in advance for your help and let me know if you need further information.

  • First off, this should really be an array, so try $view->set_arguments(array(35));. Also, have you checked to see if PHP's loose typing is affecting this, eg, $view->set_arguments('35'); vs $view->set_arguments(35);. And finally, just make sure that the nids that don't show up are actually published, the user has access to them, and of the node type being filtered (eg, does this work for admin but not other users). You can also go into the Views UI and preview the View, putting in your arguments and seeing what sql is generated for the various nids and see if something is amiss. – Jimajamma Feb 26 '14 at 16:30
  • Jimajamma I had checked everything BUT the arguments. When you set arguments as arrays, everything works fine. It is strange though that it worked with some nodes and not with others. Anyway, I really appreciate your help. If you write your comment as an answers I will mark it as accepted answer. – José L. Feb 26 '14 at 18:57
2

The argument to the $views->set_arguments() function should be an array containing the arguments, even if only one, so, try:

$view->set_arguments(array(35));

or

$view->set_arguments(array($nid));

instead of passing the value in directly.

As to why it was working before sometimes, I am betting it was working with nids 1 thru 9 only, because there, for example, if $nid='4', $nid[0]=='4' is also true, but if $nid='35', $nid[0]=='35' is false (it is '3') but I leave this to another discussion, but look up "php strings as arrays" if you want to delve into it further. This Strings as Arrays Q&A gives a brief description/introduction.

| improve this answer | |
  • Instructive :-) – José L. Feb 26 '14 at 20:36

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.