2

私はウェブサイトに数百の記事を持っています。ビューを使用して抜粋とそれらへのリンクを1ページに表示します。また、一度に10件の記事を表示するポケットベルもあります。年のドロップダウンを追加する必要があります[...、2008,2009 ...、2013]。ドロップダウンで年を選択すると、その年に投稿された記事のみが表示されます。たとえば、2013年に新しい記事が追加された場合、ドロップダウンの年は自動的に更新される必要があります。したがって、最初の年は最初の発行年になります。

考えられる解決策を提案してください。

4

2 に答える 2

10

ビューリストに公開フィルターを設定する必要があると思います。構成は次のようになります-

フィルタ基準:日付(ノード);

フォーム要素:選択;

フィルタの粒度:年;

日付フィールド:投稿日; 公開;

公開するフィルタータイプ:シングル;

演算子:等しい;

それが機能するかどうか私に知らせてください。

于 2013-03-06T07:05:42.750 に答える
1

Drupalの組み込みの「Authoredon」フィールドをノードに使用する場合は、次の方法があります。カスタムモジュールを作成する必要があります。これはDrupal7/Views3で作成されました。私のモジュールファイルは/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の回答に基づいてコードを作成しました。

于 2014-01-28T16:49:13.293 に答える