0

Thanks to this tutorial, I cam up with this block-generating module:

function myblockmodule_block_info(){
    $block['myadblocks'] = array(
        'info' => t('My first ad blcoks'),
        'cache' => DRUPAL_NO_CACHE,
    );
    return $block;
}

/**
 * Define what our block is going to look like.
 * Implements hook_block_view().
 */
function myblockmodule_block_view($block_key){
    $block = array();

    if($block_key == 'myadblocks'){ //We only want to define the content of OUR block
        //This is the title of the block.
        $block['subject'] = t('First ad block comes here');

        //Define the block content.
        $block['content'] = 'some fancy script comes here';
    }

    return $block;
}

I am wondering how to extend the module to create 2 blocks instead of one?

2 Answers 2

2

Simple as:

function myblockmodule_block_info(){
    $block['myadblocks'] = array(
        'info' => t('My first ad blcoks'),
        'cache' => DRUPAL_NO_CACHE,
    );

    $block['myadblocks2'] = array(
        'info' => t('My second ad blcoks'),
        'cache' => DRUPAL_NO_CACHE,
    );
    return $block;
}

/**
 * Define what our block is going to look like.
 * Implements hook_block_view().
 */
function myblockmodule_block_view($block_key){
    $block = array();

    if($block_key == 'myadblocks'){ //We only want to define the content of OUR block
        //This is the title of the block.
        $block['subject'] = t('First ad block comes here');

        //Define the block content.
        $block['content'] = 'some fancy script comes here';
    }
    elseif ($block_key == 'myadblocks2') {
      $block['subject'] = t('Foo');
      $block['content'] = 'var';
    }

    return $block;
}

And clear the caches.

0
2
function myblockmodule_block_info(){
    $blocks = array();
    $blocks['myadblocks'] = array(
        'info' => t('My first ad blcoks'),
        'cache' => DRUPAL_NO_CACHE,
    );
    $blocks['myadblocks2'] = array(
        'info' => t('My second ad block'),
        'cache' => DRUPAL_NO_CACHE,
    );
    return $blocks;
}

/**
 * Define what our block is going to look like.
 * Implements hook_block_view().
 */
function myblockmodule_block_view($block_key){
    $block = array();

    if($block_key == 'myadblocks'){ //We only want to define the content of OUR block
        //This is the title of the block.
        $block['subject'] = t('First ad block comes here');

        //Define the block content.
        $block['content'] = 'some fancy script comes here';
    } elseif ($block_key == 'myadblocks2') {

        $block['subject'] = t('Second ad block comes here');

        $block['content'] = 'some fancy script comes here';
    }

    return $block;
}
4
  • Seems Clive got in first :)
    – 2pha
    Commented Jan 15, 2015 at 13:39
  • Nah @AyeshK, we just picked the same array key ;)
    – Clive
    Commented Jan 15, 2015 at 13:39
  • Sorry I thought it was a 1:1. Didn't realize OP used the same block keys.
    – AKS
    Commented Jan 15, 2015 at 13:41
  • Thanks man. I chosen Clove as he responded a bit sooner. I appreciate if you comment on the follow up question: drupal.stackexchange.com/questions/144342/…
    – edi
    Commented Jan 15, 2015 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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