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

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.

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;
}
  • Seems Clive got in first :) – 2pha Jan 15 '15 at 13:39
  • Nah @AyeshK, we just picked the same array key ;) – Clive Jan 15 '15 at 13:39
  • Sorry I thought it was a 1:1. Didn't realize OP used the same block keys. – AyeshK Jan 15 '15 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 Jan 15 '15 at 14:17

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.