2017-06-12 203 views
0

所以我纔剛剛開始學習Drupal,所以如果你相信我會以這種錯誤的方式去做,請告訴我。Drupal自定義模塊HTML

我有一個名爲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

我覺得首先你需要了解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。不要忘記在創建模板後清除主題註冊表。

希望它有幫助!

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