2013-05-05 64 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

相關問題