Drupal Tips: How to create a related content (nodes) view block in Drupal 7

| | 2 min read

Are you looking to create a block of nodes related to one of the taxonomy terms in your current node? Just take an easy way to get this done on your Drupal site.

For Drupal 7, follow these steps:

  1. Create a custom content type for nodes eg:mobile. Add fields (title, a small image, date, whatever you like).Set one field for taxonomy term reference.
  2. Add vocabulary terms in to taxonomy.
  3. Create a view for related content(nodes) as a block.
  4. Add the fields (whatever you like) which has to be displayed in the related content block.
  5. Filter criteria :
    • Set the content type mobile
    • Set the content which is to be published
    • Set the taxonomy word. (Add taxonomy terms in the taxonomy manager ) Add the filter: "Content: Has taxonomy term ID".
  6. Pager: Set the no of nodes.
  7. Create a custom module block for showing the view. Steps for creating a custom module:
    • Create a folder in sites/all/modules folder - name the folder eg:my_module
    • Creating my_module.info files to provide Drupal with module information Add this code into .info files :
      
      name = my_module
      description = This is a demo module for the Drupal
      core = 7.x
      files[] = my_module.module
      
      

    • Creating my_module.module files to store Drupal code
      
      /*
      ** Implementation of hook_block_info()
      */
      
       function my_module_block_info() {
         $blocks['related_custom_block'] = array(
        'info' => t('Related content'),
        'cache' => DRUPAL_NO_CACHE,
        );
       return $blocks;
       }
      
      

      /*
      ** Implementation of hook_block_view()
      */
      
       function my_module_block_view($delta = '') {
       $block = array();
       switch ($delta) {
         case 'related_custom_block':
           $block['subject'] = t('Related content');
           if (arg(0) == 'node' && arg(1) != '') {
             $node = node_load(arg(1));
             if ($node->type == 'content_type') {
       //content_type please write your content type name
               $array_term = array();
               foreach($node->field_taxonomy_category['und'] AS $key => $value) { 
      // write your taxonomy term machine name instead of 'field_taxonomy_category'
                $array_term[] = $value['tid'];
               }
               $view = views_get_view('you_might_also_like'); 
      //write your view name
               $display_id = 'block';
               $view->set_display($display_id);
               $filter = $view->get_item($display_id, 'filter', 'field_taxonomy_category_tid');
      // write your taxonomy term machine name instead of 'field_taxonomy_category_tid'
               $filter['value'] = $array_term;
               $view->set_item($display_id, 'filter', 'field_taxonomy_category_tid', $filter);
      // write your taxonomy term machine name instead of 'field_taxonomy_category_tid'
               $viewsoutput = $view->render();
               $block['content'] = $viewsoutput;
             }
             else {
               $block['content'] = '';
             }
           }
           else {
             $block['content'] = '';
           }    
         break;
      }
       return $block;
      }
      
      

  8. Enable the custom module my_module .
  9. Save the view .
  10. Go to block listing .
  11. Assign the block into the region, and you are done.