I need to alter a Views relationship contextually: since the View is embedded into another node, the relationship is defined by a field's value of a parent node.

I presume the only way to add a relationship is using hook_views_data_alter, but I'm not quite sure if it's going to work inside a preprocessor function (I'm creating a View for a certain node type) and what's the proper way of using it. The related field will be a Nodequeue instance (Nodequeue module is integrated with Views using relationships), its name will be contained in that parent node's field.

Thanks in advance!

1 Answer 1


I did eventually discover simple solution, so perhaps it comes in handy for anyone else later. views_data_alter, of course, is not meant to apply an existing relationship, but rather to create a new one. To apply an existing relationship programmatically - Nodequeque in my case - you need to call in add_item function of view class:

$view->add_item($view->current_display, 'relationship', 'node', 'nodequeue_rel', array('names'=>array('myqueue'=>'myqueue'), 'required'=>1, 'limit'=>1))

where an array is a set of options like: the name(s) of queue(s) and whether any nodes excluded from chosen queque(s) should be added (1 - bool - implies that only nodes inside the chosen queque will be added).

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.