2013-03-05 55 views
1

我在一個網站上有數百篇文章。我使用視圖在一個頁面上顯示摘錄和鏈接,同時我也有一個顯示10篇文章的傳呼機。我需要添加一個有多年[...,2008,2009 ...,2013]的下拉菜單。如果您在下拉列表中選擇一年,則查看應該只顯示那年發佈的文章。例如,如果在2013年添加了新文章,則下拉的年份應該會自動更新,因此第一年是首次發佈的年份。Drupal 7:在發佈日期公開的過濾器

請提出可能的解決方案。

回答

9

我認爲,您需要將暴露過濾器設置爲視圖列表。配置應該類似 -

過濾標準:日期(節點);

表單元素:選擇;

篩選粒度:年份;

日期欄位:發佈日期;暴露;

過濾器類型公開:Single;

運算符:等於;

通知我,如果它的工作原理..

+0

我安裝了Date模塊,然後基本上按照您的意見,它的工作原理。謝謝 – 2013-03-14 20:53:16

+1

您需要安裝date_views才能正常工作 – anthonygore 2015-11-10 23:21:20

1

這裏是一個辦法做到這一點,如果你想使用Drupal內置的「撰寫了」字段的節點。您需要創建一個自定義模塊。這是在Drupal 7/Views 3中創建的。我的模塊文件位於/ sites/all/modules/custom/ViewsYearFilter /中。

ViewsYearFilter.info

name = Views Year Filter 
description = Allow a view to filter by post year. 
package = tools 
core = 7.x 

files[] = ViewsYearFilter.inc 
files[] = ViewsYearFilter.views.inc 

ViewsYearFilter.module

<?php 

/** 
* Implements of hook_views_api(). 
*/ 
function ViewsYearFilter_views_api() { 
    return array('api' => 3); 
} 

ViewsYearFilter.views.inc

<?php 

/** 
* Implements of hook_views_data(). 
*/ 
function ViewsYearFilter_views_data() { 
    return array(
    'node' => array(
     'published_year' => array(
     'group' => t('Content'), 
     'title' => t('Year'), 
     'help' => t('Filter by years.'), 
     'filter' => array('handler' => 'ViewsYearFilter'), 
    ) 
    ) 
); 
} 

ViewsYearFilter.inc

<?php 

/* Allows filtering of posts by year in a Drupal View. */ 

class ViewsYearFilter extends views_handler_filter_in_operator { 

    /** 
    * Override parent get_value_options() function. This function returns an array of all valid years from our post type. 
    * @return 
    * Return the stored values in $this->value_options if someone expects it. 
    */ 
    function get_value_options() { 

    $query = new EntityFieldQuery(); 
    $query->entityCondition('entity_type', 'node') 
    ->propertyCondition('type', 'blog_post') //post type 
    ->propertyCondition('status', '1'); //is published 

    $results = $query->execute(); 

    $object_node_ids = array_keys($results['node']); 
    $objects = node_load_multiple($object_node_ids); 

    foreach ($objects as $blog_post) { 
     $values[date('Y', $blog_post->created)] = date('Y', $blog_post->created); 
    } 

    $this->value_options = $values; 
    return $values; //array of year values 
    } 

    function query() { 
    $this->ensure_my_table(); //not sure why/if this is necessary 
    $startDate = mktime(0, 0, 0, 1, 1, intval($this->value[0])); 
    $endDate = mktime(0, 0, 0, 1, 1, intval($this->value[0] + 1)); 
    $this->query->add_where_expression($this->options['group'], "node.created >= " . $startDate . " AND node.created <= " . $endDate); //filtering query 
    } 

} 

然後,在您的視圖配置頁面,您可以創建一個新的過濾器暴露:

對不起,但我不能真的發表了這一形象,因爲我沒有足夠的聲譽。將會有一個新的名爲「Content:Year」的暴露過濾器,您可以在創建並激活新模塊後添加到您的視圖中。

PS:我根據Shevchuk在this question上的回答得到了我的代碼。