Here comes the views handler for D7.
Create a folder for custom modules sites/all/modules/custom
and therein create a folder for our module. Let's call it nodetype_filter
. Now you save the following four lines as nodetype_filter.info
file inside this folder to tell Drupal about our new module (sites/all/modules/custom/nodetype_filter/nodetype_filter.info
):
name = Nodetype Filter
description = Adds a Views contextual filter to display only nodes of current node's type
core = 7.x
dependencies[] = views
Create a .module
file in our module's folder (sites/all/modules/custom/nodetype_filter.module
) and implement hook_views_api()
by pasting in the following code:
<?php
/**
* Implementation of hook_views_api().
*/
function nodetype_filter_views_api() {
return array(
'api' => 3 // this number depends on what version of Views you are using
);
}
Once hook_views_api()
is implemented, Views will automatically look for a file named [module].views.inc
in our module's home directory. So let's create it by saving the following code inside sites/all/modules/custom/nodetype_filter.views.inc
:
<?php
/**
* Implements hook_views_plugins().
*/
function nodetype_filter_views_plugins() {
return array(
'argument default' => array(
'param' => array(
'title' => t("custom: current node's type"),
'handler' => 'nodetype_filter_plugin_argument_default_param',
),
),
);
}
/**
* Default argument plugin to use the current node's type.
*
* @ingroup views_argument_default_plugins
*/
class nodetype_filter_plugin_argument_default_param extends views_plugin_argument_default {
function get_argument() {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
return $value = $node->type;
} else {
return $value = FALSE;
}
}
}
Finally, activate your module and edit your view. Add a contextual filter Content: type
. Select Provide a default value
and select the filter custom: current node's type
we created before.
Please feel free to help correcting my code if anything is wrong since I didn't test it altogether, but wrote a similar module earlier.