以编程方式创建多个块


9

我正在为Drupal 7编写一个自定义模块。我需要用该模块创建5个不同的块。Drupal具有hook_block_infohook_block_view挂钩来创建块。由于这些挂钩每个模块只允许一个块,我如何添加多个块?

Answers:


19

以下代码应该有所帮助。显然,您需要适当地添加5个块,但这只是说明如何在一个模块中添加多个块:

function MYMODULE_block_info() {
 $blocks = array();
 $blocks['myfirstblock'] = array(
   'info' => t('My block admin info'),
   'status' => 1,
 );
 $blocks['mysecondblock'] = array(
   'info' => t('My second block admin info'),
   'status' => 1,
 );

 return $blocks;
}

function MYMODULE_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'myfirstblock':
      $block = array(
        'subject' => t('My first block title'),
        'content' => t('My first block content'),
      );
      break;
    case 'mysecondblock':
      $block = array(
        'subject' => t('My second block title'),
        'content' => t('My second block content'),
      );
      break;
  }

  return $block;
}

原因status => 1hook_block_info()API文档:

status :(可选)块启用状态的初始值。(1 =启用,0 =禁用)。大多数模块不提供初始值,并且用户可以在块组态屏幕上修改提供的任何值。

我不确定每个模块的想法是从哪里得到的。您可以在模块中创建任意数量的块。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.