Optional Spaces Integration for a View
If you want to make an exported [node-based] View smoothly integrate with Spaces, you can use the following code to modify the View structure with the “Content in current space” filter. This filter does nothing if the View is not itself in a Space, otherwise it restricts all results to content in the same space. Add any conditions you want to control whether the Spaces integration is applied.
/** * Implementation of hook_views_default_views_alter(). */ function custom_views_default_views_alter(&$views) { if (module_exists('spaces')) { $views['view_name_here']->display['default']->display_options['filters']['current'] = array( 'operator' => 'all', 'value' => '', 'group' => '0', 'exposed' => FALSE, 'expose' => array( 'operator' => FALSE, 'label' => '', ), 'id' => 'current', 'table' => 'spaces', 'field' => 'current', 'relationship' => 'none', ); } }
If you would like to take it a step further, and create OpenAtrium features integration, insert the following:
$views['view_name_here']->display['default']->display_options['access'] = array( 'type' => 'atrium_feature', 'spaces_feature' => 'feature_name', 'perm' => 'access content', // or any other permission );
hook_views_default_views_alter() fires off before the View is cached, so be sure to clear your cache after adding this code to see the results.
This code snippet is a finding from my work on OpenAtrium-FeatureServer integration.
2 comments
Or, of course, you could just
Or, of course, you could just use the built-in “Node is in this space” filter…..
That's What This Does
Hello Stranger,
The code snippet above does exactly that. However, it allows you to do it without modifying the view directly.
This allows you to add the View into Spaces with additional logic of your choice, to avoid broken filter handlers if someone is using the View but not the Spaces module, and lastly to integrate a View with a Spaces-driven site without needing to maintain an override.
Instead, you can maintain an _alter hook implementation, which I feel is a better approach. Of course, if the View is one you’ve created yourself, build it to your needs. If, on the other hand, you are trying to incorporate someone else’s maintained module or feature into your site, modifying the View is not that different in principle from hacking core.