3

I have two custom node types : catalog_item and category.

The catalog_item node has a cck node reference field that allow a user to select multiple category nodes. I want to be able to select a particular category from the node reference based on some php code. Is this possible with views?

2 Answers 2

2

Within the views API, you could use this field as an argument and then use PHP to supply the default argument. This would then restrict the views to all catalog_items that have that specific category node referenced.

If you are doing this in code/a module/template/etc, you could do it also like this:

$nid=WHATEVERPHPCODEYOUWANT;
return views_embed_view(VIEWNAME,'default',$nid); // 'default' is the views display
1
  • Thanks! I ended up doing something similar to this. Iterating through the view was pretty easy.
    – James
    Mar 8, 2012 at 14:35
0

The other option is to use one of the views hooks.

I believe this will work, but I haven't tested to be sure:

function mymodule_views_pre_view (&$view, &$display_id, &$args)
{
  if ($view->name == "myview" && $view->current_display == "mydisplay") {
    $nid = some_function_to_get_the_nid();
    $view->args = array($nid);
  }
}

I don't recall if you need to set the arguments in the display handler, too.

With things like this, you need to poke through $view with dpm() and experiment with the various hooks.

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.