Related content in Drupal

  • strict warning: Non-static method view::load() should not be called statically in /home/tomkaname/jan.tomka.name/sites/all/modules/views/views.module on line 906.
  • strict warning: Declaration of views_handler_field_comment::init() should be compatible with views_handler_field::init(&$view, $options) in /home/tomkaname/jan.tomka.name/sites/all/modules/views/modules/comment/views_handler_field_comment.inc on line 49.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/tomkaname/jan.tomka.name/sites/all/modules/views/handlers/views_handler_filter.inc on line 607.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/tomkaname/jan.tomka.name/sites/all/modules/views/handlers/views_handler_filter.inc on line 607.
  • strict warning: Declaration of views_handler_filter_node_status::operator_form() should be compatible with views_handler_filter::operator_form(&$form, &$form_state) in /home/tomkaname/jan.tomka.name/sites/all/modules/views/modules/node/views_handler_filter_node_status.inc on line 13.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/tomkaname/jan.tomka.name/sites/all/modules/views/plugins/views_plugin_row.inc on line 134.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/tomkaname/jan.tomka.name/sites/all/modules/views/plugins/views_plugin_row.inc on line 134.
  • strict warning: Non-static method view::load() should not be called statically in /home/tomkaname/jan.tomka.name/sites/all/modules/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/tomkaname/jan.tomka.name/sites/all/modules/views/handlers/views_handler_argument.inc on line 744.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/tomkaname/jan.tomka.name/sites/all/modules/views/handlers/views_handler_filter_boolean_operator.inc on line 159.
  • strict warning: Non-static method view::load() should not be called statically in /home/tomkaname/jan.tomka.name/sites/all/modules/views/views.module on line 906.
  • strict warning: Non-static method view::load() should not be called statically in /home/tomkaname/jan.tomka.name/sites/all/modules/views/views.module on line 906.
  • strict warning: Declaration of calendar_plugin_display_page::options_submit() should be compatible with views_plugin_display_page::options_submit(&$form, &$form_state) in /home/tomkaname/jan.tomka.name/sites/all/modules/calendar/includes/calendar_plugin_display_page.inc on line 297.
  • strict warning: Declaration of calendar_plugin_display_page::options() should be compatible with views_object::options() in /home/tomkaname/jan.tomka.name/sites/all/modules/calendar/includes/calendar_plugin_display_page.inc on line 297.
  • strict warning: Declaration of calendar_plugin_display_block::options() should be compatible with views_object::options() in /home/tomkaname/jan.tomka.name/sites/all/modules/calendar/includes/calendar_plugin_display_block.inc on line 78.
  • strict warning: Declaration of calendar_plugin_display_attachment::options_submit() should be compatible with views_plugin_display_attachment::options_submit(&$form, &$form_state) in /home/tomkaname/jan.tomka.name/sites/all/modules/calendar/includes/calendar_plugin_display_attachment.inc on line 242.
  • strict warning: Declaration of calendar_plugin_display_attachment::options() should be compatible with views_object::options() in /home/tomkaname/jan.tomka.name/sites/all/modules/calendar/includes/calendar_plugin_display_attachment.inc on line 242.
  • strict warning: Declaration of date_api_argument_handler::init() should be compatible with views_handler_argument::init(&$view, &$options) in /home/tomkaname/jan.tomka.name/sites/all/modules/date/includes/date_api_argument_handler.inc on line 398.
  • strict warning: Declaration of calendar_view_plugin_style::options() should be compatible with views_object::options() in /home/tomkaname/jan.tomka.name/sites/all/modules/calendar/includes/calendar_view_plugin_style.inc on line 173.

I started using the Related Content module a few days ago and was a bit disappointed that for such a simple functionality no proper module seems to be around. While working perfectly a couple of things really got me thinking about how this could have been done better.

Using Taxonomy and Views you can achieve the exact same behavior with what I believe is a more intuitive configuration interface requiring less effort to manage your website's related content.

Related content Block view

Related Content

My issues with the Related Content module are the quite unintuitive setup in the beginning and mainly the unnecessary separate tab to define related content. I'd love that to be part of the node edit form.

Also, the two-way related content linking means that every time you add a node, you have to go to the existing nodes that are to be related and define the relation to the new node. That is just unacceptable. I want the related content to be configured solely from the content node being created. The easiest way is to define a taxonomy as a means of content grouping. That way, all nodes that share a node term from that taxonomy are considered related.

The worst of all, the Related Content's list block requires the block cache disabled. Nah, I'm putting something together myself!

Related content revamped

The idea is to replace the module with either flags or tags to create a group of related content nodes. When new content is being added, user just picks a group from list directly in the node edit page. I reckon taxonomy provides more flexibility than the Flag module,

Taxonomy

Create new vocabulary called Related content. Make it applicable to your content types you want to have the related content functionality available for, e. g. Story in my case.

Related content Vocabulary

Depending on your preferences, set up the vocabulary to serve as a related content group selector. I'm going for Tags, so that I can make use of autocompletion and also create new groups on fly. I don't expect to have content in multiple groups, but Multiple is always checked for Tags. Lastly, related content is going to be an optional content property, I don't want to enforce the related content group selection, thus I leave the Required unchecked. To have the Related content group input control always as the last one in the taxonomy configuration block, I set the Weight to 10. If I wanted to be fancy about it, I'd use taxonomy_content to place it underneath the content body block.

I add a couple of terms I know I'm going to use straight away, and those are Drupal tips and Pomodoro Technique.

Content

Now edit the existing content to set the Related content group for each relevant node. There's not that much content I've got so far, so I'm doing it manually. You can use the Taxonomy Manager module to make your life easier.

Views

We're going to create a view with block display, which will list all node titles linked to their nodes, sharing the same term in our Related content vocabulary. I'll leave the basic setup with you, your preferences and requirements. The only important part of the view, when it comes to our related content functionality, is two arguments and their default values.

Using argument to exclude the current node

It's quite logical that in the list of the currently viewed node's related content we don't want to list the node itself. That is very easy to achieve by following these few steps:

  1. Add an argument for Node: nid
  2. Set 'Action to take if argument is not present' to 'Provide default argument'
  3. Set 'Default argument type' to 'Node ID from URL'
  4. Check 'Exclude the argument'

Related content Argument Node ID

Using argument to include nodes with same term

The second argument will help us to retrieve all nodes sharing the Related content vocabulary term with current node.

  1. Add an argument for Taxonomy: Term ID (with depth)
  2. Set 'Action to take if argument is not present' to 'Provide default argument'
  3. Set 'Default argument type' to 'PHP Code'
  4. Insert the following snippet:
    $node = node_load(arg(1));
    $terms = taxonomy_node_get_terms_by_vocabulary($node, 4);
    $term = current($terms);
    return $term->tid;

To make the PHP code use all vocabulary terms, not just the first one, which is sufficient for me, the code would look a bit like this:

node = node_load(arg(1));
$terms = taxonomy_node_get_terms_by_vocabulary($node, 4);
$tids = array();
foreach ($terms as $term)
  $tids[] = $term->tid;
return $tids;

Related content Argument Term ID (with depth)

My Related content's vocabulary VID is 4, and that's the second, hardcoded parameter to the taxonomy_node_get_terms_by_vocabulary() function call. You can work out your vocabulary's VID in the taxonomy administration pages. Edit vocabulary page will have the vocabulary's VID in it's URL.

Watch out! Return value of the taxonomy_node_get_terms_by_vocabulary() function is an associative array with the term id's used for keys. That means, you can't just do $terms[0]->tid. May seem obvious to you, I've just spent an hour learning the hard way...

Also, the argument type must be Term ID (with depth). Plain Term ID would work too, but we want to make use of the percent substitution to dynamicaly set the view title. With the latter, we'd end up with the actual numeric Term ID instead of the term name.

To have the view title dynamically contain the related content group, set the arguments Title to something along the lines of "Related in %2", where %2 will get replaced with the term name.

That's it

Place the block wherever you see fit, in the sidebar or append it to the end of the content for your users to have somewhere to go when done with the current story.

After that, all you have to do for the related content functionality to be useful, is to add your nodes to respective related content groups. And the best part is that you only have to do it once.

Comments

Nice article. Cheers, Victor

Nice article.

Cheers,
Victor

I too was disappointed with

I too was disappointed with the Related Content module, but I found a good alternative in Relevant Content (http://drupal.org/project/relevant_content). Have you tried this module? I'm using it successfully on both Cross Union (http://crossunion.com/) and SQL Tidbits (http://sqltidbits.com/). Just browse to any node and you will side a sidebar block with the 5 most relevant nodes based on how many taxonomy terms they share in common. You can split out by taxonomy and have as many different blocks as you want. The one limitation that I see is that I can't give certain taxonomies a higher weight than others, but otherwise it works great.

How about adding a bit info

How about adding a bit info about your next big, big life project?

I have a taxonomy hierarchy,

I have a taxonomy hierarchy, this is causing all taxonomy in the hierarchy to be related,

Asia
Thailand
Phuket

Where as I only need the last in the hierarchy (Phuket). Is this possible?

Would really REALLY appreciate any help you can offer.

Thanks in advance.

Mike

It certainly is possible.

It certainly is possible. You're gonna have to change the PHP snippet from the tutorial. There are two in the tutorial -- one picks just the very first term, the other one picks all, which must be your case. You need to make it return only terms that are not parents of other terms in the hierarchy. Have a look at taxonomy_get_tree() and the pair of {term}, {term_hierarchy} DB tables.

Brilliant ! thank you !! :-D

Brilliant ! thank you !! :-D

Thank you for sharing this

Thank you for sharing this with us.
It's an excellent tutorial and got me exactly what I needed.

Thanks again.

Featured Content Module

Featured Content Module ...

You may want to try out the Featured Content module at

http://www.drupal.org/project/featured_content

for your related content blocks as it is geared for non-technical users and is, thus, made to be simple and intuitive with inline descriptions/documentation. You can check out the user ratings at drupalmodules.com here:

http://drupalmodules.com/module/featured-content

Cheers,
Kristen