我为Drupal 7构建了一个自定义块,用户可以在块中显示任何节点,从选择任何内容类型并选择显示任意数量的节点:
我设法让它能够显示节点并选择要显示的内容类型。唯一的问题是它们中有多少要显示。
代码示例是:
function custom_block_configure() {
$types = node_type_get_types();
foreach ($types as $node_type) {
$nodetypes[$node_type->type] = $node_type->name;
}
$form['example_nodes_toshow'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the nodes to show'),
'#options' => $nodetypes,
'#default_value' => variable_get('example_nodes_toshow', array('')),
'#description' => t('All the node types selected will be shown'),
);
$form['example_number_display'] = array(
'#type' => 'textfield',
'#title' => t('Enter number of node to display in block'),
'#default_value' => variable_get('example_number_display', array('')),
'#description' => t('Display any number of nodes'),
'#size' => 5,
);
return system_settings_form($form);
}
所有表单都以块配置显示。第一种形式是供用户选择显示内容类型而第二种形式仅供选择显示多少种形式,因此从1 - 5开始。
在此之后我不确定它是否正确,我没有完成example_number_display的任何代码,但你在那里帮助我。
下一个代码是:
function example_block_save($delta = '', $edit = array(), $number_limit = array()) {
if ($delta == 'customblock') {
variable_set('example_nodes_toshow', $edit['example_nodes_toshow']);
variable_set('example_number_display', $number_limit['example_number_display']);
}
}
下一个代码是block_view,其中包含一些用于创建节点列表并显示它的查询:
function example_block_view($block_name = '') {
if ($block_name == 'exampleblock') {
//Get the selected node types and create a list of them
$show_nodes_list = array();
$show_nodes_array = variable_get('example_nodes_toshow', array(''));
foreach ($show_nodes_array as $key => $value) {
if ($value) {
array_push($show_nodes_list, $value);
}
}
global $user;
//Based on the node types create a query and then load the node types
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', $show_nodes_list)
->propertyCondition('status', 1)
->propertyCondition('uid', $user->uid)
->propertyOrderBy('created', 'DESC')
->range(0, 5);
$result = $query->execute();
$nodes = array();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$nodes = entity_load('node', $nids);
}
//Loop through the loded nodes to create a list
$list = array();
$i = 0;
foreach ($nodes as $node) {
$options = array('absolute' => TRUE);
$url = url('node/' . $node->nid, $options);
$list[$i] = '<a href=' . $url . '>' . $node->title . '</a>';
$i++;
}
//Return the content of the block
$theme_args = array('items' => $list, 'type' => 'ol');
$content = theme('item_list', $theme_args);
$block = array(
'subject' => t('Show Nodes Block'),
'content' => $content,
);
return $block;
}
}
在abowe代码中我插入范围(0,5)来限制块中节点的显示但是我无法设法将它与example_number_display连接或者是否有任何其他简单的解决方案。
答案 0 :(得分:2)
是的,您可以管理与“example_number_display”相关联的范围
Instead of static value
->range(0, 5);
You can use
$numberdisplay = variable_get('example_number_display', '5');
->range(0, $numberdisplay);