Download & Extend

Views default argument from Context

This module allows a default value for a view argument to be specified based on a piece of contextual data set through the Context module's context_set function. When configuring the default argument, you specify the context namespace and the attribute name, and the value is pulled automatically. This is particularly useful for getting information to Views blocks. This is far less hackish than specifying path arguments via arg().

Example use case

Let's say we have a Views block. It's supposed to list all of the nodes that are nodereferenced to whatever node is specified in the argument to the view like so:

nid:23
|_____nid:45
|_____nid:29
|_____nid:35
etc.

Because there's no good way to send arguments to blocks, the traditional approach is to set the View's "Default argument" parameter to PHP code containing:

<?php
return arg(1);
?>

...or something similar. It is hackish and terrible to rely on the URL for the current nid -- that approach fails to keep the control hierarchy intact (node/xxx invokes a node controller, and if views is invoked somewhere in that stack, it should not be able to jump all the way up to the node controller to retrieve its arguments -- the node or page controller should supply those arguments).

The proper approach is to allow a URL to invoke a controller (like hook_nodeapi) that then arranges all the data to be displayed in the view. So, in the example given above, you would add a line to your helper module's hook_nodeapi that calls

<?php
context_set
('namespace', 'attribute', $node->nid);
?>

Then, you can enter the namespace and attribute you chose into the Views plugin, and the nid will be transmitted as an argument to your block.

You can easily use this for string arguments as well; for example, if you need to filter based on node type, you could call context_set('namespace', 'attr', 'blogpost'), etc.

Another applications of this module -- making blocks of reverse node references, i.e., a list of nodes that reference the current one. (if you think of other applications, let me know and I'll be glad to add them!)

Credits

  • This module was developed by brynbellomy.
  • The module was the subject of a DrupalCon 2010 UnConference session given by sethcohn. Thanks for the support, Seth!

Downloads

Recommended releases

Version Downloads Date Links
6.x-1.3 tar.gz (9.83 KB) | zip (11.59 KB) 2010-Oct-25 Notes

Development releases

Version Downloads Date Links
7.x-2.x-dev tar.gz (11.19 KB) | zip (12.98 KB) 2012-Apr-13 Notes
6.x-1.x-dev tar.gz (9.72 KB) | zip (11.38 KB) 2011-Feb-25 Notes

Project Information


Maintainers for Views default argument from Context

  • xjm - 1 commit
    last: 1 year ago, first: 1 year ago
  • brynbellomy - 9 commits
    last: 2 years ago, first: 3 years ago

Issues for Views default argument from Context

To avoid duplicates, please search before submitting a new issue.
All issues
Bug reports
Statistics (2 years)
New issues
Open bugs
Participants