3

Question: I have a content type "VIDEOS" and have added 20 videos - 1 through 20. I want to achieve a functionality like if you are watching video x, the next 3 videos are showing in the sidebar, example -> when you are watching video 2 in a block in the sidebar videos 3, 4, 5 are showing up. When you watch video 7 - in the sidebar videos 8, 9 10 are showing up and so on. Any ideas? I know it will be done somehow with Views but cannot figure it out.

BR, Ivan Ivanov

5

I would do that with a little code in a custom module...

For the purposes of this example my module is called misc, but replace everywhere is says misc with the name of your your module.

First create a block:

function misc_block_info() {
    $blocks['next_four'] = array(
        'info' => t('Show the next 4'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
    );
    return $blocks;
}

And then define where it's content comes from:

function misc_block_view($delta = '') {
    switch ($delta) {
        case 'next_four':
            $block['subject'] = '';
            $block['content'] = next_four();
            break;

    }
    return $block;
}

And then define the content of the block:

function next_four() {
    $output = '';
    if(arg(0)=='node') {
        $node = node_load(arg(1));
        $query = db_select('node', 'n')
            ->fields('n', array('nid'))
            ->condition('n.type', $node->type)
            ->condition('n.status', 1)
            ->condition('n.nid', $node->nid, '>')
            ->range(0, 4)
            ->orderBy('n.nid', 'ASC');
        $nids = $query->execute()->fetchCol();
        $nodes = node_load_multiple($nids);

        $output .= '<ul>';
        foreach($nodes as $node) {
            $output .= '<li><a href="'.drupal_get_path_alias('node/'.$node->nid).'">'.$node->title.'</a></li>';
        }
        $output .= '</ul>'; 
    }
    return $output;
}

What this code does is:

  1. Take the nid of the current node
  2. Does a query to get more nodes of the same type with an nid height than the current page.
  3. Loads each node and outputs the node title, with a link to the node.

This will work for any content type. If your HTML is going to be any more complex than this then I would suggest using the hook_theme function to create a template file for your block.

| improve this answer | |
  • Hoped to do it via Views as this is a bit advanced for me. I will try it though and will let you know the result. Much appreciated. Cheers :) – Ivan Ivanov Apr 11 '13 at 15:30
  • 1
    I've put together all of that code into a custom module for you so you can just install it and it'll work out the box: felixeve.co.uk/next_x_items.zip - take a look at the code too - should be some good example if you want to learn more about module development in the future... – Felix Eve Apr 11 '13 at 15:49
  • 1
    U rock man ;) will try it tomorrow morning and will share... – Ivan Ivanov Apr 11 '13 at 16:18
  • to do it in views you will need a "great than" contextual filter: drupal.stackexchange.com/questions/35128/… – uwe Apr 12 '13 at 14:54

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.