0

I am unable to change the date format for an exposed field in views. I am using BEF datepicker popup. I overrode the jquery settings to get it to enter a UK date in the dd-mm-yy format but because views is expecting it in mm-dd-yy form, the results of the filter are wrong.

This problem is addressed in this discussion https://www.drupal.org/node/1409120 but supposedly has been fixed for other people in Date 7.x 2.x. I am using BEF 7.x-3.0-beta4, Date 7.x-2.9-alpha1+2-dev, and Views 7.x-3.8, and the problem isn't fixed. I can't see any extra options in the view settings or in the config menu to change the date format.

The field name is field_activity_date_time and I have created a custom module with the following code, based on other stackoverflow suggestions (I don't have a max and min field, just a single date)

function hook_form_views_exposed_form_alter(&$form, &$form_state) {
    if($form['#id'] == 'views-exposed-form-test-page') {
       $form['field_activity_date_time']['#date_format'] = 'd/m/Y';
      }
    }
}

I have tried a number of different possibilities for the array key, such as 'activity_date_time', and 'views_field_activity_date_time', none of which worked. This date field is coming from a CiviCRM table which may be why the other suggestions haven't worked, but it feels to me more of a drupal problem than a civicrm one.


as suggested by harsh in the comments, I have used a different hook, but the problem still persists...

function HOOK_form_alter(&$form, &$form_state, $form_id){ 
  if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'test' && $form_state['view']->current_display == 'page') { 
     $form['activity_date_time']['#date_format'] = 'd/m/Y';
     echo '<pre>';
     print_r($form);
     echo '</pre>';
 }
}

This gave me the following

Array
(
    ...

    [activity_date_time] => Array
        (
            [#type] => textfield
            [#size] => 30
            [#default_value] => 
            [#attributes] => Array
                (
                    [class] => Array
                        (
                            [0] => bef-datepicker
                        )

                )

            [#date_format] => d/m/Y
        )
)
7
  • you can try this else if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'abc_view_name' && $form_state['view']->current_display == 'page') Aug 19, 2014 at 11:48
  • Using dpm() i added your code and tested which if statement it was executing, but it only enters my if statement in the OP.
    – ankles
    Aug 19, 2014 at 12:12
  • you have to add form alter, in that fnction call this if statement where you can find the view name by using export your view display name in advance section Aug 19, 2014 at 12:15
  • I did that, exporting the view to get the name (simply 'test') and still nothing... full code else if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'test' && $form_state['view']->current_display == 'page') { dpm($form); }
    – ankles
    Aug 19, 2014 at 12:24
  • 1
    function MYMODULE_form_alter(&$form, &$form_state, $form_id){ if ($form_id == 'views_exposed_form' && $form_state['view']->name == 'test' && $form_state['view']->current_display == 'page') { print_r($form);exit; }} Aug 19, 2014 at 12:27

1 Answer 1

1

Well, I am not proud of the clunky fix but it works!

function gmcvo_date_format_views_query_alter (&$view, &$query){
    $newDate = DateTime::createFromFormat('Y-d-m H:i:s', $query->where[1]['conditions'][0]['value']);
    $query->where[1]['conditions'][0]['value'] = $newDate->format('Y-m-d H:i:s');
}

If someone has a better way to do this it would be great to hear it...

edited to use the new datetime object instead of strtotime

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.