所以我才开始学习Drupal所以如果你认为我这样做的方法不对,请告诉我。
我有一个名为Events的内容类型。我基本上只是想在主页上输出最新事件的片段。为此,我在Drupal教程Drupal doc's custom module tutorial
之后创建了一个自定义模块这是我的模块代码
<?php
/**
* Implements hook_block_info().
*/
function latest_event_block_info() {
$blocks['latest_event'] = array(
// The name that will appear in the block list.
'info' => t('Latest Event'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Custom content function.
*
* Set beginning and end dates, retrieve posts from database
* saved in that time period.
*
* @return
* A result set of the targeted posts.
*/
function latest_event_contents(){
//Get today's date.
$today = getdate();
//Calculate the date a week ago.
$start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']);
//Get all posts from one week ago to the present.
$end_time = time();
//Use Database API to retrieve current posts.
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'event')
->propertyCondition('status', 1) // published == true
->propertyCondition('created', array($start_time, $end_time), 'BETWEEN')
->propertyOrderBy('created', 'DESC') //Most recent first.
->range(0, 1); //ony grab one item
return $query->execute();
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function latest_event_block_view($delta = '') {
switch ($delta) {
case 'latest_event':
$block['subject'] = t('Latest Event');
if (user_access('access content')) {
// Use our custom function to retrieve data.
$result = latest_event_contents();
$nodes = array();
if (!empty($result['node'])) {
$nodes = node_load_multiple(array_keys($result['node']));
}
// Iterate over the resultset and generate html.
foreach ($nodes as $node) {
//var_dump($node->field_date);
$items[] = array(
'data' => '<p>
<span class="text-color">Next Event</span> ' .
$node->field_date['und'][0]['value'] . ' ' .
'</p>' .
'<p>' .
$node->title . ' ' .
$node->field_location['und'][0]['value'] . ' ' .
'</p>'
);
}
// No content in the last week.
if (empty($nodes)) {
$block['content'] = t('No events available.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items));
}
}
return $block;
}
}
我已将该块添加到某个区域,并且在我的模板页面中呈现正常。但是,事件以列表的形式输出,这不是我想要的。
以下是如何在HTML中呈现块
<div class="item-list">
<ul>
<li class="first last">
<p>
<span class="text-color">Next Event</span> June 23 2016 18:30 - 21:00 </p><p>Cancer Research UK Angel Building, 407 St John Street, London EC1V 4AD
</p>
</li>
</ul>
</div>
所以假设我正确地处理了这一切,我怎么能修改这个块html?谢谢!
答案 0 :(得分:0)
它正在输出列表,因为您正在传递$block['content']
item_list主题功能。
您可以使用hook_theme创建自己的自定义主题模板。这将允许您在自定义模板文件中使用自定义标记。
之后,替换它:
// Pass data through theme function.
$block['content'] = theme('item_list', array('items' => $items));
有了这个:
// Pass data through theme function.
$block['content'] = theme('my_custom_theme', array('items' => $items));
答案 1 :(得分:0)
我认为首先你需要了解theme('item_list', ....)。这总是输出一个HTML列表,无论是UL还是OL。
如果您想在没有HTML列表包装器的情况下显示您的内容,可以试试这个:
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function latest_event_block_view($delta = '') {
switch ($delta) {
case 'latest_event':
$block['subject'] = t('Latest Event');
if (user_access('access content')) {
// Use our custom function to retrieve data.
$result = latest_event_contents();
$nodes = array();
if (!empty($result['node'])) {
$nodes = node_load_multiple(array_keys($result['node']));
}
// Iterate over the resultset and generate html.
$output = '';
foreach ($nodes as $node) {
//var_dump($node->field_date);
$output .= '<p>
<span class="text-color">Next Event</span> ' .
$node->field_date['und'][0]['value'] . ' ' .
'</p>' .
'<p>' .
$node->title . ' ' .
$node->field_location['und'][0]['value'] . ' ' .
'</p>';
}
// No content in the last week.
if (empty($output)) {
$block['content'] = t('No events available.');
}
else {
// Pass data through theme function.
$block['content'] = $output;
}
}
return $block;
}
}
这是一种方式。另一种方法是使用您自己的自定义模板并使用该数组通过它输出。例如。
// Pass data to template through theme function.
$block['content'] = theme('latest_event_block_template', $items);
然后定义一个hook_theme函数以将其转换为模板,例如:
function latest_event_theme() {
return array(
'latest_event_block_template' => array(
'arguments' => array('items' => NULL),
'template' => 'latest-event-block-template',
),
);
}
现在,您应该在模块的根目录中有一个名为latest-event-block-template.tpl.php
的模板。在此模板上,您将能够获取$ items数组并自行调整HTML。在创建模板后不要忘记清除主题注册表。
希望它有所帮助!