0

I have a simple date (birthday) field in my View, and I have added that field as an exposed filter. I want to be able to filter by just the month, so that the View can return a list of users whose birthdays are in the month of July, for example. Currently, the exposed filter also requires that I choose a year, which makes the filter un-usable. How can I filer based on just the month and nothing else?

Thanks!

0

You can use Computed field and create other field on your content type, there Month will stored (calculated as PHP code: return date('m', {Date Field Value});, so you can easily filter by months in Views by this field...

| improve this answer | |
  • Nikit, can you please elaborate on your answer? The Computed Field gives me two text areas: "Computed Code (PHP)" and "Display Code (PHP)". In which field do I place your code, and what would I put in the other field? My date field is called "field_birthday". Thanks! – hockey2112 Jun 26 '14 at 21:51
  • hockey2112, in computed code put next: return date('m', {HERE YOU SHOULD FOUND PLACEHOLDER OF YOUR DATE FIELD}); In display code you can use the same, it'snot usefult for your task... – Nikit Jun 27 '14 at 23:53
0

You can do this by following some steps

Add these steps to youe view-

Firstly add date field as an simple filter, then add same date field as an exposed filter, then add these lines to header of the view

<form method="get">
<select name="field_date_value">
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">Aug</option>
<option value="09">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
<input type="submit" value="Apply">
</form>

After adding above steps save the view.

Now write below code to your activated theme template.php file

function hook_views_query_alter(&$view, &$query)
{
    if($view->name=='demo12') // in place of demo12 ,place your view name here
    {
        if(isset($_GET['field_date_value']))
        {
        $month=$_GET['field_date_value'];

        $query->where[1]['conditions'][2]['field']="DATE_FORMAT(ADDTIME(field_date_value
, SEC_TO_TIME(19800)),'%m') = :field_data_field_date_field_date_value";   


        $query->where[1]['conditions'][2]['value'][':field_data_field_date_field_date_value']=$month;
        }
        else
        {
            $query->where[1]['conditions']=array();
            $query->where[1]['conditions'][0]['field']='node.status';
            $query->where[1]['conditions'][0]['value']='1';
            $query->where[1]['conditions'][0]['operator']='=';
            $query->where[1]['conditions'][1]['field']='node.type';
            $query->where[1]['conditions'][1]['value'][0]='demo';
            $query->where[1]['conditions'][1]['operator']='in';
        }
    }

}

like- my theme name is 'testing' so replace "hook_views_query_alter(&$view, &$query)"

to "testing_views_query_alter(&$view, &$query)" in template.php.

You have to change fields name in above code as 'field_date_value' in above code is my content type 'date field' machine name.so you have to change it. The above code is working in my view.

You can import the below given code of view to you site:-

======================================================================================

 $view = new view();
    $view-name = 'demo12';
    $view-description = '';
    $view-tag = 'default';
    $view-base_table = 'node';
    $view-human_name = 'demo12';
    $view-core = 7;
    $view-api_version = '3.0'

;
    $view-disabled = FALSE;  Edit this to true to make a default view disabled initially 

 Display Master 
$handler = $view-new_display('default', 'Master', 'default');
$handler-display-display_options['title'] = 'demo12';
$handler-display-display_options['use_more_always'] = FALSE;
$handler-display-display_options['access']['type'] = 'perm';
$handler-display-display_options['cache']['type'] = 'none';
$handler-display-display_options['query']['type'] = 'views_query';
$handler-display-display_options['exposed_form']['type'] = 'basic';
$handler-display-display_options['pager']['type'] = 'full';
$handler-display-display_options['pager']['options']['items_per_page'] = '10';
$handler-display-display_options['style_plugin'] = 'default';
$handler-display-display_options['row_plugin'] = 'fields';
 Header Global Text area 
$handler-display-display_options['header']['area']['id'] = 'area';
$handler-display-display_options['header']['area']['table'] = 'views';
$handler-display-display_options['header']['area']['field'] = 'area';
$handler-display-display_options['header']['area']['empty'] = TRUE;
$handler-display-display_options['header']['area']['content'] = 'form method=get
select name=field_date_value
option value=01Janoption
option value=02Feboption
option value=03Marchoption
option value=04Apriloption
option value=05Mayoption
option value=06Juneoption
option value=07Julyoption
option value=08Augoption
option value=09Sepoption
option value=10Octoption
option value=11Novoption
option value=12Decoption
select
input type=submit value=Apply
form';
$handler-display-display_options['header']['area']['format'] = 'full_html';
 Field Content Title 
$handler-display-display_options['fields']['title']['id'] = 'title';
$handler-display-display_options['fields']['title']['table'] = 'node';
$handler-display-display_options['fields']['title']['field'] = 'title';
$handler-display-display_options['fields']['title']['label'] = '';
$handler-display-display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler-display-display_options['fields']['title']['alter']['ellipsis'] = FALSE;
 Field Content Image 
$handler-display-display_options['fields']['field_image']['id'] = 'field_image';
$handler-display-display_options['fields']['field_image']['table'] = 'field_data_field_image';
$handler-display-display_options['fields']['field_image']['field'] = 'field_image';
$handler-display-display_options['fields']['field_image']['click_sort_column'] = 'fid';
$handler-display-display_options['fields']['field_image']['settings'] = array(
  'image_style' = '',
  'image_link' = 'content',
);
 Field Global Custom text 
$handler-display-display_options['fields']['nothing']['id'] = 'nothing';
$handler-display-display_options['fields']['nothing']['table'] = 'views';
$handler-display-display_options['fields']['nothing']['field'] = 'nothing';
$handler-display-display_options['fields']['nothing']['alter']['text'] = 'a class=colorbox-load href=httpwww.youtube.comwatchv=617ANIA5Rqs title= We Share Our Mother's HealthWatch Nowa';
 Field Content BirthDate 
$handler-display-display_options['fields']['field_date']['id'] = 'field_date';
$handler-display-display_options['fields']['field_date']['table'] = 'field_data_field_date';
$handler-display-display_options['fields']['field_date']['field'] = 'field_date';
$handler-display-display_options['fields']['field_date']['settings'] = array(
  'format_type' = 'long',
  'fromto' = 'both',
  'multiple_number' = '',
  'multiple_from' = '',
  'multiple_to' = '',
);
 Sort criterion Content Post date 
$handler-display-display_options['sorts']['created']['id'] = 'created';
$handler-display-display_options['sorts']['created']['table'] = 'node';
$handler-display-display_options['sorts']['created']['field'] = 'created';
$handler-display-display_options['sorts']['created']['order'] = 'DESC';
 Contextual filter Content BirthDate (field_date) 
$handler-display-display_options['arguments']['field_date_value']['id'] = 'field_date_value';
$handler-display-display_options['arguments']['field_date_value']['table'] = 'field_data_field_date';
$handler-display-display_options['arguments']['field_date_value']['field'] = 'field_date_value';
$handler-display-display_options['arguments']['field_date_value']['default_argument_type'] = 'date';
$handler-display-display_options['arguments']['field_date_value']['summary']['number_of_records'] = '0';
$handler-display-display_options['arguments']['field_date_value']['summary']['format'] = 'default_summary';
$handler-display-display_options['arguments']['field_date_value']['summary_options']['items_per_page'] = '25';
 Filter criterion Content Published 
$handler-display-display_options['filters']['status']['id'] = 'status';
$handler-display-display_options['filters']['status']['table'] = 'node';
$handler-display-display_options['filters']['status']['field'] = 'status';
$handler-display-display_options['filters']['status']['value'] = 1;
$handler-display-display_options['filters']['status']['group'] = 1;
$handler-display-display_options['filters']['status']['expose']['operator'] = FALSE;
 Filter criterion Content Type 
$handler-display-display_options['filters']['type']['id'] = 'type';
$handler-display-display_options['filters']['type']['table'] = 'node';
$handler-display-display_options['filters']['type']['field'] = 'type';
$handler-display-display_options['filters']['type']['value'] = array(
  'demo' = 'demo',
);
 Filter criterion Content BirthDate (field_date) 
$handler-display-display_options['filters']['field_date_value']['id'] = 'field_date_value';
$handler-display-display_options['filters']['field_date_value']['table'] = 'field_data_field_date';
$handler-display-display_options['filters']['field_date_value']['field'] = 'field_date_value';
$handler-display-display_options['filters']['field_date_value']['granularity'] = 'month';

 Display Page 
$handler = $view-new_display('page', 'Page', 'page');
$handler-display-display_options['path'] = 'demo12';

===========================================================================================

You have to create content type with name 'demo' for it.Or have to change the name 'demo' with your content type name.

May this code will help you

| improve this answer | |
  • TBI Infotech, I tried to incorporate your method, but I could not get it to work. I'm sure I am doing something wrong. Would you please provide me with the proper code snippets with my view name ("users") and the name of my date field ("field_birthday")? Thanks! – hockey2112 Jun 26 '14 at 22:50
0

I ended up discovering the Birthdays module (https://www.drupal.org/project/birthdays). It provides Views filters on the year, month, and day as separate filters. I simply had to export my existing date info as a CSV, parse the data out into separate columns to match the format of the birthdays database table, ad import that date into the birthdays database table.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.