【问题标题】:Exposed date filter in Drupal - making "month" optionalDrupal 中的公开日期过滤器 - 使“月”可选
【发布时间】:2013-05-05 16:38:20
【问题描述】:

我的网站上有一个视图,其中列出了一个视频存档和一个具有年/月粒度的公开过滤器。我的问题是过滤器只接受同时选择年份和月份值的输入,但我确实需要让用户能够按年份过滤而不必选择月份,并且能够先选择年份然后再选择如果他们愿意,可以通过按月过滤来优化搜索。

我是 Drupal 初学者,所以我对 Drupal 的基础架构了解不多。我什至不知道视图存储在哪里。如果我这样做了,也许我可以以某种方式更改代码。

【问题讨论】:

    标签: drupal-7 drupal-views


    【解决方案1】:

    我不确定是否有内置方法可以使月份成为可选月份,但这是一种可能的解决方法。您可以添加两个公开的过滤器,一个具有年粒度,一个具有年-月粒度。然后你可以使用hook_form_FORM_ID_alter 来改变暴露的表单(一定要添加一个条件来检查它是你的视图和显示ID)。您可以添加一个验证回调,以便在提交表单时,如果选择了月份,您可以在 year_month 字段中设置年份。

    我没有对此进行测试,但这通常是我处理 form_alter 的方式。

    <?php
    function my_module_form_views_exposed_form_alter(&$form, &$form_state) {
      $view = $form_state['view'];
      if ($view->name == 'my_view' && $view->current_display == 'my_display') {
        // Assuming the year exposed filter is 'year' and year-month exposed filter
        // is 'year_month'.
        $form['year_month']['value']['year']['#access'] = FALSE; // Hides the year
        $form['#validate'][] = 'my_module_my_view_filter_validate';
      }
    }
    
    function my_module_my_view_filter_validate($form, &$form_state) {
      $values = isset($form_state['values']) ? $form_state['values'] : array();
      // When the month is set, grab the year from the year exposed filter.
      if (isset($values['year_month']['value']['month'])) {
        // If the year is not set, we have set a user warning.
        if (!isset($values['year']['value']['year'])) {
          drupal_set_message(t('Please select a year.'), 'warning');
        }
        else {
          // Otherwise set the year in the year_month filter to the one from our
          // year filter.
          $year = $values['year']['value']['year'];
          $form_state['values']['year_month']['value']['year'] = $year;
        }
      }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      相关资源
      最近更新 更多