Drupal 7 Drupal自定义模块HTML

Drupal 7 Drupal自定义模块HTML,drupal-7,drupal-modules,drupal-blocks,drupal-templates,Drupal 7,Drupal Modules,Drupal Blocks,Drupal Templates,所以我才刚刚开始学习Drupal,如果你认为我走错了方向,请告诉我 我有一个称为事件的内容类型。我基本上只是想在主页上输出最新事件的片段。为此,我在Drupal教程之后创建了一个自定义模块 这是我的模块代码 <?php /** * Implements hook_block_info(). */ function latest_event_block_info() { $blocks['latest_event'] = array( // The name that w

所以我才刚刚开始学习Drupal,如果你认为我走错了方向,请告诉我

我有一个称为事件的内容类型。我基本上只是想在主页上输出最新事件的片段。为此,我在Drupal教程之后创建了一个自定义模块

这是我的模块代码

 <?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;
  }

}

它作为一个列表输出,因为您正在传递
$block['content']
主题函数

相反,您可以使用创建自己的自定义主题模板。这将允许您在自定义模板文件中使用自定义标记

之后,请更换此:

// 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));

我认为首先你需要了解这个问题。这总是输出一个给定的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;
  }

}
然后定义一个hook_主题函数,将其添加到模板中,如:

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。不要忘记在创建模板后清除主题注册表

希望有帮助

// Pass data to template through theme function.
$block['content'] = theme('latest_event_block_template', $items);
function latest_event_theme() {
  return array(
    'latest_event_block_template' => array(
      'arguments' => array('items' => NULL),
      'template' => 'latest-event-block-template',
    ),
  );
}