0

I created one custom module, it will create one custom block when it is installed. I want to display it on "product" content type only. Can I do the same with programmatically. It'll save my time. Here is my code.

<?php
function my_module_block_info() {
    $blocks = array();

    $blocks['my_module_config'] = array(
            'info' => t('My Module'),
    );
    return $blocks;
}


/**
 * Implements hook_block_view().
 */
function my_module_block_view($delta = '') {
    $block = array();

    switch ($delta) {
        case 'my_module_config':
            $my_module_form = drupal_get_form('my_module_form');
            $my_module_form = drupal_render($my_module_form);
            $block['subject'] = '';
            $block['content'] = $my_module_form;
            break;
    }
    return $block;
}
?>
3
  • I've tried to improve code format, but I didn't get idea. :-) Commented Aug 3, 2015 at 13:05
  • No options available to set content type custom. You need to go for admin >> Structure >> Blocks configure and at bottom you find content type you would like to display.
    – vgoradiya
    Commented Aug 3, 2015 at 13:21
  • @VirangJethva to format the code of a question/answer you need to add 4 spaces before the text
    – M a m a D
    Commented Aug 4, 2015 at 7:09

4 Answers 4

1

This code should work for you

function my_module_block_info() {
  $blocks = array();
  $blocks['my_module_config'] = array(
    'info' => t('My Module'),
    'status' => TRUE,
    'region' => 'content',
  );
  return $blocks;
}


/**
 * Implements hook_block_view().
 */
 function my_module_block_view($delta = '') {
 $block = array();

 switch ($delta) {
   case 'my_module_config':
     $block['subject'] = '';
     $node = menu_get_object();
     if (isset($node->type) && $node->type == 'product') {
       $my_module_form = drupal_get_form('my_module_form');
       $my_module_form = drupal_render($my_module_form);
       $block['content'] = $my_module_form;
     }
     else {
       $block['content'] = '';
     }
     break;
  }
  return $block;
}
1
  • Thanks Andrew, but this won't work. Commented Aug 4, 2015 at 6:11
1

I got one solution but It's weird, It's working by this way.

function example_install() {
// Install our custom block visibility settings per node type
$query = db_insert('block_node_type')

->fields(array('type', 'module', 'delta'))
->values(array(
  'type' => 'my-node-type', // content-type
  'module' => 'example', // My module name
  'delta' => 'my-block-delta', // Same delta used in hook_block_info
))
->execute(); 
}
0

Clearly you know how to do it using admin UI in block configuration, If you don't want to use this option, Why don't you load the block in Product node pages instead of assigning it's visibility to this content type in the module?


Did you try hook_block_list_alter ?

This hook allows you to add, remove or modify blocks in the block list. The block list contains the block definitions, not the rendered blocks. The blocks are rendered after the modules have had a chance to manipulate the block list.

The Show block for specific content type programmatically can be helpful. The hook_block_list_alter_example.php is another example.

3
  • Hi Drupalist, I'm creating one custom module, it'll create one block and when it is installed then it will assigned to one product content type. I want to do these all process when it is installed. Commented Aug 3, 2015 at 13:25
  • @VirangJethva I updated my answer
    – M a m a D
    Commented Aug 3, 2015 at 13:32
  • Yes I tried hook_block_list_alter. Commented Aug 3, 2015 at 14:08
0

As I can see, the Drupal core save the block content types setting in function node_form_block_admin_configure_submit(). I copied the code from node.module file. So I copied it and made my one here:

  // Say I have two blocks want to show on 'article' content type.
  $my_blocks = array(
    // Block 1: module is 'views' and delta is 'views-block-1'.
    array(
      'type' => 'article',
      'module' => 'views',
      'delta' => 'views-block-1',
    ),
    // Block 2: module is 'webform' and delta is 'client-block-1'.
    array(
      'type' => 'article',
      'module' => 'webform',
      'delta' => 'client-block-1',
    ),
  );

  // Insert settings into 'block_node_type' table.
  $query = db_insert('block_node_type')->fields(array('type', 'module', 'delta'));
  foreach ($blocks as $block) {
    $query->values(array(
      'type' => $block['type'],
      'module' => $block['module'],
      'delta' => $block['delta'],
    ));
  }
  $query->execute();

Notice: Above code should only run once. I putted it into my module update script(my_module.install). Otherwise you may need to add some checking to prevent duplicated records in 'block_node_type' table.

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.