第一个Drupal模块-将PHP脚本转换为Drupal 7模块


First Drupal Module - converting PHP script to Drupal 7 Module

我正试图转换一个常用的PHP脚本,它已经为我服务了一段时间,但现在我正试图将其转换为一个模块,以方便使用,并为挑战。它将一个块放入列表中,但不会将任何内容放入块中。因为我是一个HTML5/CSS3的极客,所以这个PHP对我来说有点新。有人能在这里指导我吗?多谢

<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']}&deg;F</span>
                    <br/>
                    <img src="http://l.yimg.com/a/i/us/we/52/{$current['code']}.gif" style="vertical-align: middle;"/>&nbsp;
                    {$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>

Drupal通过钩子系统工作,你可以重写,所以你已经覆盖了hook_help, hook_block_info和hook_block_view,但是没有hook_weather(好吧,至少在核心Drupal中没有,尽管一个贡献的模块可能会提供一个),所以目前moduleone_weather()从未被调用。

您可以通过放置$output = moduleone_weather();在检查hook_block_view函数中$output是否为空之前。