Drupal 自定义模块 HTML
Drupal Custom Module HTML
所以我才刚刚开始学习 Drupal,所以如果您认为我的做法有误,请告诉我。
我有一个名为“事件”的内容类型。我基本上只是想在主页上输出最新事件的片段。为此,我按照 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?谢谢!
它作为列表输出,因为您正在传递 $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));
我想首先你需要了解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。创建模板后不要忘记清除主题注册表。
希望对您有所帮助!
所以我才刚刚开始学习 Drupal,所以如果您认为我的做法有误,请告诉我。
我有一个名为“事件”的内容类型。我基本上只是想在主页上输出最新事件的片段。为此,我按照 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?谢谢!
它作为列表输出,因为您正在传递 $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));
我想首先你需要了解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。创建模板后不要忘记清除主题注册表。
希望对您有所帮助!