我正在尝试转换一个很常用的PHP脚本,这个脚本已经很好地作为一个块了一段时间,但现在我正在尝试将其转换为易于使用的模块,以及挑战。它将一个块放入列表中,但不将任何内容放入块中。因为我更像是一个HTML5 / CSS3极客,这对我来说有点新鲜。谁能帮助我在这里指导我?非常感谢
<code>
<?php
/**
* @file
* A block module that displays a local weather forcast for Reading Berks UK.
*/
function moduleone_help($path, $arg) {
switch ($path) {
case "admin/help#moduleone":
return '<p>' . t("Displays the weather for a user defined region defined by <a href='#'>LINK</a>") . '</p>';
break;
}
}
/**
* Implement hook_block_info().
*/
function moduleone_block_info() {
$blocks['moduleone'] = array(
// The name that will appear in the block list.
'info' => t('Module One Weather'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Custom content function.
*
* Get weather report from Yahoo
* Output var is $output.
*
* @return
* A result set of the targeted posts.
*/
function moduleone_weather(){
$zipcode = 'UKXX0117';
$result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=f');
$xml = simplexml_load_string($result);
//echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
$location = $xml->channel->xpath('yweather:location');
if(!empty($location)){
foreach($xml->channel->item as $item){
$current = $item->xpath('yweather:condition');
$forecast = $item->xpath('yweather:forecast');
$current = $current[0];
$output = <<<END
<h1 style="margin-bottom: 0">Weather for {$location[0]['city']}, {$location[0]['region']}</h1>
<small>{$current['date']}</small>
<h2>Current Conditions</h2>
<p>
<span style="font-size:72px; font-weight:bold;">{$current['temp']}°F</span>
<br/>
<img src="http://l.yimg.com/a/i/us/we/52/{$current['code']}.gif" style="vertical-align: middle;"/>
{$current['text']}
</p>
<h2>Forecast</h2>
{$forecast[0]['day']} - {$forecast[0]['text']}. High: {$forecast[0]['high']} Low: {$forecast[0]['low']}
<br/>
{$forecast[1]['day']} - {$forecast[1]['text']}. High: {$forecast[1]['high']} Low: {$forecast[1]['low']}
<br/>
{$forecast[2]['day']} - {$forecast[2]['text']}. High: {$forecast[2]['high']} Low: {$forecast[2]['low']}
</p>
END;
}
}else{
$output = 'No results found, please try a different zip code.';
}
function moduleone_block_view($delta = '') {
switch ($delta) {
case 'moduleone':
$block['subject'] = t('Weather Conditions');
if (user_access('access content')) {
}
if (empty($output)) {
$block['content'] = t('No forcast available.');
}
else {
$block['content'] = theme('item_list', array(
'items' => $output));
}
}
return $block;
}
}</code>
答案 0 :(得分:0)
Drupal通过你覆盖的钩子系统工作,所以你正好覆盖了hook_help,hook_block_info和hook_block_view但是没有hook_weather(好吧,至少不在核心Drupal中,尽管贡献的模块可以提供一个)所以在时刻moduleone_weather()永远不会被调用。
你可以通过放置$ output = moduleone_weather();在你的hook_block_view函数中检查$ output是否为空之前。