2013-05-05 63 views
2

我在我的网站上有一个视图,其中列出了视频存档和具有年/月粒度的公开过滤器。我的问题是,过滤器只接受年份和月份值都被选中的输入,但我真的需要使用户能够逐年过滤,而不必选择月份,并且能够先选择年份,然后再选择年份如果他们想按月过滤,则按月进行过滤。Drupal中暴露的日期过滤器 - 使“月”可选

我是Drupal初学者,所以对Drupal的基础设施知之甚少。我甚至不知道视图的存储位置。如果我这样做了,也许我可以以某种方式修改代码。

回答

3

我不确定是否有一个内置的方法来使该月可选或不可以,但这是一个可能的解决方法。您可以添加两个暴露的过滤器,一个具有年份粒度,另一个具有年份粒度。然后,您可以使用来更改公开的表单(请务必添加一个条件来检查它是您的视图并显示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; 
    } 
    } 
} 
?> 
+0

谢谢,我会让你知道它是否有效。我相信会的。 – alouette 2013-05-07 17:08:19

相关问题