How to allow multiple blocks in a module of Drupal(如何在 Drupal 的模块中允许多个块)
问题描述
我正在尝试为 Drupal 7 制作一个模块,该模块提供一个块并具有某些配置设置.现在我想要的是我想向用户提供 5 个块,以便他们可以在每个块中使用不同的设置.换句话说,我想为每个块提供一组单独的设置.我该怎么做?
I am trying to make a module for Drupal 7 that provides a block and has certain configuration settings. Now what I want is that I want to provide 5 blocks to the user so that they can use different settings in each block. In other words, I want to provide each block a separate set of settings. How can I do that?
实际上我已经制作了一个显示单个块的模块.如果您使用过 superfish 菜单模块,那么您可以在那里看到它们允许我们选择应提供多少块可用.这样对于每个块我们可以使用不同的菜单来显示.我说的是那个功能
Actually I have made a module that shows a single block. If you have used superfish menu module then you can see there that they allow us an option to choose how many block should be made available. So that for each block we can use different menu to show. I am talking about that functionality
推荐答案
创建配置页面:
function my_module_admin_settings($form, &$form_state) {
$form['my_module_number_of_blocks'] = array(
'#title' => t('Post to Blog by default'),
'#description' => t('Should content post to blog by default or only when selected?'),
'#type' => 'select',
'#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10)),
'#default_value' => 2,
);
return system_settings_form($form);
}
您使用 hook_block_info 你定义一个数组,如:
You create blocks in a module using hook_block_info you define an array like:
hook_block_info() {
number_blocks = variable_get('my_module_number_of_blocks', 0);
$x=0
while ($x < number_of_blocks) {
$blocks['myblock-' . $x] = array(
'info' => t('Block ' . $x),
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks
}
您将在 hook_block_configure:
function hook_block_configure($delta = '') {
// This example comes from node.module.
$form = array();
$parts = explode($delta, '-');
if ($parts[0] == 'my_block') {
$form['my_block_' . $parts[1] . '_option1'] = array(
'#type' => 'select',
'#title' => t('Some option'),
'#default_value' => variable_get('my_block_' . $parts[1] . '_option1', 'first_option'),
'#options' => drupal_map_assoc(array('first option', 'second option')),
);
}
return $form;
}
一旦你定义了你的块,你需要告诉他们如何使用 hook_block_view.类似的东西:
Once you have defined your blocks you need to tell them how to display with hook_block_view. Something like:
function hook_block_view($delta = '') {
$block = array();
$parts = explode($delta, '-');
if ($parts[0] == 'myblock') {
$block['subject'] = t('Block' . $parts[1]);
$block['content'] = my_module_block_b_generate($parts[1]);
}
return $block;
}
然后您将使用块号和配置来确定输出:
Then you'll use the block number and configuration to determine the output:
my_module_block_b_generate($block_number) {
$option1 = variable_get('my_block_' . $block_number . '_option1', 'first_option');
return t('this is block ' . $block_number . '. It has chosen option ' . $option1 . ' for option1');
}
这篇关于如何在 Drupal 的模块中允许多个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Drupal 的模块中允许多个块
基础教程推荐
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01