7

I have a block that is displaying related content to the node being viewed. Currently it's displaying node titles for three content types : hotel, tour operator and transport. I would like it to display only content type that is similar to the content of the node currently viewed and filter out the two other content types.

I have been trying to set this up using contextual filers in views but I can't get it to work.

Is it something that can be done using contextual filters without writing any code ? Or do I need to write some php (I can't) to get this to do what I need.

Thanks

  • I need to do the same thing is there a way to do it using contextual filters so that the block will know the node of the page its on by reading the css class if I use the context module ? – Diana Jan 16 '15 at 15:18
6

It might be possible to add a conditional filter on the node type and then add custom PHP code to get the default value based on the currently viewed node. The code to do this should look like this: (should work both in D6 and D7):

if ($node = menu_get_object()) {
  return $node->type;
}

It would be cleaner to do that in a views handler, that is however quite a bit more complicated to set up than pasting the above code in the textfield in the views UI.

  • 1
    I am interested in this method, should this code be used for "When the filter value is NOT available" or "When the filter value IS available or a default is provided"? – Danny Englander Apr 30 '12 at 16:39
  • Just stumbled upon this and need an answer as well.. – jan Dec 20 '12 at 21:13
  • When the filter value is not available. – Berdir Dec 22 '12 at 19:15
  • @Berdir Added views handler answer. Please help if you see any mistakes. – leymannx Jul 17 '14 at 15:57
2

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.

-2

Instead of using contextual filter, you can just simply just add three blocks, each for one content type (hotel, tour operator and transport).

Something like this: display hotel content listing block only for hotel node, transport listing block only for transport node.

Don't forget to exclude the current node from the block view, so it won't show duplicated content.

  • But this is not an efficient way of doing it...what if there are 50 content types, you can't make 50 different view isn't it? – SGhosh Mar 22 '13 at 9:01
  • That's not a solution :D – Alexander Kim Mar 23 '14 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.