问题描述
我正在尝试为 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 的一个模块中允许多个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!