This error pops up when you have a date popup in an exposed Views form and want Views to remember the value.

What happens:
- The form is submitted with a correct date popup value (thus as an array with date/time entries).
- The form is validated and date popup "massages" (as put in the function comments) the value into a string (in function date_popup_validate) and puts that back into the form_state['values'].
- Views stores the values in $_SESSION so it can repopulate an exposed form later on.

Later on:
- Views populates $form_state['input'] using $_SESSION, thus putting a string in the value for the date popup element
- The function date_popup_element_value_callback gets called with a string as $input, is assigned to $return after which $return['date'], that is $return[0], that is the first character of the string is set to a strange value.

Solution: I think the easiest way to solve this is to accept strings in date_popup_element_value_callback() as $input and convert them to a date/time array first before processing.

CommentFileSizeAuthor
#1 1342874-1.patch2.84 KBfietserwin

Comments

fietserwin’s picture

Status: Active » Needs review
StatusFileSize
new2.84 KB

Attached a patch that solves this problem. Some notes about the patch:

- You can't give a 2nd parameter a default value and then pass in a 3rd parameter (by reference and without default).
- $return will be assigned a value at the end of the function. Any earlier assignment will be overwritten and is thus useless.
- The lines to get a date object form a string are copied from function date_popup_input_date().
- After the problem was solved in function date_popup_element_value_callback(), a validation problem occurred at function date_popup_validate(), so I added code to accept a string there as well.

tim.plunkett’s picture

Title: date popup in exposed Views form cannot "remember" value » Allow date popup in exposed Views form to 'remember' value
Category: bug » feature
Status: Needs review » Needs work
+++ date_popup/date_popup.module	Wed Nov 16 10:26:26 2011
@@ -229,23 +229,27 @@
-function date_popup_element_value_callback($element, $input = FALSE, &$form_state) {
+function date_popup_element_value_callback($element, $input, &$form_state) {

This is just a mistake in the parameters, it should be date_popup_element_value_callback($element, $input = FALSE, $form_state = array())

+++ date_popup/date_popup.module	Wed Nov 16 10:26:26 2011
@@ -229,23 +229,27 @@
-  $return = $has_time ? array('date' => '', 'time' => '') : array('date' => '');

What was wrong with this line?

+++ date_popup/date_popup.module	Wed Nov 16 10:26:26 2011
@@ -229,23 +229,27 @@
+    $format .= $has_time ? ' ' . date_popup_time_format($element) : '';

This is a confusing ternary. It should just be a 3 line `if` statement for clarity.

+++ date_popup/date_popup.module	Wed Nov 16 10:26:26 2011
@@ -229,23 +229,27 @@
-  $return['date'] = date_is_date($date) && !$date->timeOnly ? $date->format(date_popup_date_format($element)) : '';
-  $return['time'] = date_is_date($date) && $has_time ? $date->format(date_popup_time_format($element)) : '';
+  $return = array (
+    'date' => date_is_date($date) && !$date->timeOnly ? $date->format(date_popup_date_format($element)) : '',
+    'time' => date_is_date($date) && $has_time ? $date->format(date_popup_time_format($element)) : '',

Changes like this make the patch harder to review.

gynekolog’s picture

thx