Drupal 7:发布日期的暴露过滤器

Posted

技术标签:

【中文标题】Drupal 7:发布日期的暴露过滤器【英文标题】:Drupal 7: Exposed filter on a post date 【发布时间】:2013-03-05 22:16:47 【问题描述】:

我在一个网站上有数百篇文章。我使用视图在一个页面上显示摘录和指向它们的链接,并且我还有一个寻呼机,当时显示 10 篇文章。我需要添加一个带有年份 [...,2008,2009...,2013] 的下拉列表。如果您在下拉列表中选择年份,则视图应仅显示该年份发布的文章。例如,如果在 2013 年添加了一篇新文章,下拉列表中的年份应该会自动更新,因此第一年是首次发表的年份。

请提出可能的解决方案。

【问题讨论】:

【参考方案1】:

我认为,您需要将暴露过滤器设置为视图列表。配置应该类似于--

过滤条件:日期(节点);

表单元素:选择;

过滤粒度:年份;

日期字段:发布日期;暴露;

要公开的过滤器类型:Single;

运算符:等于;

如果它有效,请告诉我..

【讨论】:

我安装了 Date 模块,然后基本上遵循了您的 cmets 并且它可以工作。谢谢 您需要安装 date_views 才能正常工作【参考方案2】:

如果您想为节点使用 Drupal 内置的“Authored on”字段,这是一种方法。您需要创建一个自定义模块。这是在 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
  


然后,在您的视图配置页面中,您可以创建一个新的暴露过滤器:

抱歉,由于我没有足够的声誉,我实际上无法发布此图片。在创建和激活新模块后,您可以将一个名为“内容:年份”的新公开过滤器添加到您的视图中。

PS:我的代码基于 Shevchuk 对 this question 的回答。

【讨论】:

以上是关于Drupal 7:发布日期的暴露过滤器的主要内容,如果未能解决你的问题,请参考以下文章

Drupal 暴露日期过滤器:从选择中删除“-Year”

Drupal 视图暴露的过滤器日期时间弹出窗口

Drupal 视图 UI,过滤器暴露,集合字段内容中的日期仅按月和年(无天)

Drupal 7 视图暴露的过滤器

Drupal 7 - 暴露过滤器 - 全文搜索

Drupal 7 视图暴露的过滤器不起作用