0

I have content type event that has date field and i created view "events" to this content type and added contextual filter "Date".

The problem is in the database the field date value is stored with this format 2007-01-17T00:00:00 and I want to filter like this events/2007. Unfortunately, I get no results because the URL should be like this events/2007-01-17T00:00:00.

So what is the solution for making it work with this events/2007 by altering the query using views_views_query_alter() instead of creating field that hold the value like this 2007. I believe that altering the query by this way should solve it but it didn't

function views_views_query_alter(&$view, &$query) {
  if ($view->name == 'events' && is_numeric(arg(1)) ) {
    $view->query->where[0]['conditions'][3]['field']=" YEAR('field_data_field_date.field_date_value')";    
  }
}
4
  • Mohamad, the edit was to improve the readability of the question, which in turn will get you a better answer. Formatted as-is, some people won't even bother reading the question or will edit it
    – Laxman13
    Sep 6, 2011 at 15:35
  • hello mohamad salama, also I suggest to try to give format your question, for example for your code. is just add some spaces before each line in your code.
    – cigotete
    Sep 6, 2011 at 18:39
  • Mohamad, remember to use functions to test/debug your functions, for example, if you have Devel module, you can use dsm() to output values that are you working (or you can use the very helpful print_r or the echo).
    – cigotete
    Sep 6, 2011 at 21:48
  • i already debug the query & the results and when i add 'YEAR' sql function to the field 'field_date_value' by doing so $view->query->where[0]['conditions'][3]['field']=" YEAR('field_data_field_date.field_date_value')"; it doing something wrong to the query is gota be some way else for doing that. Sep 7, 2011 at 12:48

1 Answer 1

1

Which Drupal and Views versions? From what I can see on my test site when creating:

A Filter Criteria you can specify granularity to year; same thing goes for a Contextual filter. (D7, latest Views).

1
  • yes its D 7.7 & View 7.x-3.0 all i want to do something that hook the criteria field to make it YEAR('field_date_value') instead of just 'field_date_value3' Sep 7, 2011 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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