0

I have a module of type block.Now based on the user i want to display different blocks . So i have a written the module but my dilemma is if its possible to call a block(default) inside our custom block? Here's my code m trying !

<?php
/**
 * Implements hook_block_info().
 */
function sample_module_block_info() {
  $blocks['current_posts'] = array(
    'info' => t('Current Users Of the Drupal Website'), //The name that will appear in the block list.
    'cache' => DRUPAL_CACHE_PER_ROLE, //Default
  );
  return $blocks;
}
function sample_module_block_view($delta = '')
 { 

 $block = array();
global $user;
  switch ($delta) {
    case 'current_posts':
      $block['subject'] = t('Logged in User Currently');
$block['content']=$user->name;
      break;
/**
 switch ($delta) {
 case 'current_posts': 
global $user;
print "The current user is: ";
print $user->name;
if($user->name == "admin")
print " This Shit Works ";
*/
    }
  return $block;
}
   ?>

Now in block['content'] i want to display another block! Can somebody lead me to how to do this ? If possible ?

2

Yes, you can load any block

....
$block['content']= sample_module_block_content();
....
function sample_module_block_content(){
  $block = block_load('module_name', 'block_name');      
  return _block_get_renderable_array(_block_render_blocks(array($block)));        
}
  • Ya your code is awesome if we have a module of our own but I want to call a default block which doesn't come in module.Do you have any idea how to do that? – Saurav Apr 30 '13 at 15:56
  • if you want call standard block : module_name can be block or system, user, node, search. Block_name - find on blocks page. You can check all blocks names and it's modules in database in table 'block', check two columns 'module' and 'delta' – smile Apr 30 '13 at 20:03
0

Sample Code and works perfectly :-(Code to call a default block inside a custom module )

<?php
/**
 * Implements hook_block_info().
 */
function sample_module_block_info() {
  $blocks['current_posts'] = array(
    'info' => t('Current Users Of the Drupal Website'), //The name that will appear in the block list.
    'cache' => DRUPAL_CACHE_PER_ROLE, //Default
  );
  return $blocks;
}
function sample_module_block_view($delta = '')
 { 

 $block = array();
global $user;
  switch ($delta) {
    case 'current_posts':
      $block['subject'] = t('Logged in User Currently');
$block['content']=call_block();
      break;
/**
 switch ($delta) {
 case 'current_posts': 
global $user;
print "The current user is: ";
print $user->name;
if($user->name == "admin")
print " This Shit Works ";
*/
    }
  return $block;
}
function call_block(){
  $block = block_load('system', 'powered-by');      
  return _block_get_renderable_array(_block_render_blocks(array($block)));        
}

This is the module file and after that u need an info file of the module .

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.