问题描述
我正在尝试为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);
}
您可以使用您定义了一个数组:
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
}
您将配置中的值:
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;
}
然后,您将使用块号和配置来确定输出: / p>
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模块中的多个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!