Hi,
First of all, thanks for this fabulous module.

I would like to report some behaviour in date_popup which I am reticent to call a 'bug' because it might be how the module was intended to behave. However it seems unusually restrictive given the flexibility which is available in the rest of the module, so it might just be an oversight which has not been noticed yet.

The problem that I am experiencing is when using date popup with a value for #date_format which is different from the built in standard Y-m-d H:i:s. I want to use d-m-Y H:i:s and have specified this in the form element #date_format. The popup element works exactly as expected and after selecting from the calendar the string is shown in the text entry box correctly:

But behind the scenes, the text value assigned to the actual node field uses the standard built-in datetime format, via the following two lines in date_popup_validate()

  $value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
  form_set_value($element, $value, $form_state);

The constant DATE_FORMAT_DATETIME is 'Y-m-d H:i:s' defined in date_api.module. This means that the creation of the final string value does not respect the format requested in $element['#date_format']. Here is a log of the variable values:

I have modified the line which creates $value to use the actual format requested:

  $value = !empty($date) ? $date->format($element['#date_format']) : NULL;
  form_set_value($element, $value, $form_state);

This has the desired effect - the value in the node matches what is in the text entry fields and is formatted as per the requested #date_format

So, this works and solves the problem I was having, and I have tested it with other combinations of the date parts and all seems ok. But maybe I have missed the point - please let me know if I have! I can provide a patch if and when appropriate.

Jonathan

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jonathan1055’s picture

russellb’s picture

subscribing

jonathan1055’s picture

FileSize
65.04 KB

Hi russellb,

Thanks for your interest in this issue. Are you a user of scheduler, or is this problem affecting a different module that you use? It would be useful to know, as that may lend weight to getting it fixed.

By the way, did you know that if you want to follow an issue, instead of the old days where you had to add a 'subscribe' post, you can now click the 'follow' button at the top of the issue. The benefit of this is that it does not send an email to everyone else who is already following the post. You can also unfollow by clicking the same button.

Jonathan

russellb’s picture

Hi Jonathan,

Thanks for your reply. I thought it might be good for people on the issue to find out that we had popped up.

We are not using scheduler here, just straight date module with the popup calendar. Our problem seems to have fixed itself here since I posted.

The short version of the story is that we had trouble getting the date format of the widget to switch from MM/DD/YYYY to DD/MM/YYYY - but it's working now.

We'll keep an eye out and post if we see anything else strange going on here.

jonathan1055’s picture

Status: Active » Needs review
FileSize
457 bytes

This problem was discovered during investigation for #1720338: Allow for no time field on date popup

Here is a patch against the latest date dev _7.x-2.6+2 (dated 2012-09-30). I hope you will consider looking at this, as it is preventing us from implementing more flexible options using Date Popup.

Thanks
Jonathan

Status: Needs review » Needs work

The last submitted patch, _1855810_5.date_popup.respect_element_date_format.patch, failed testing.

jonathan1055’s picture

Status: Needs work » Needs review
FileSize
457 bytes

Try again.

Status: Needs review » Needs work

The last submitted patch, _1855810_7.date_popup.respect_element_date_format.patch, failed testing.

jonathan1055’s picture

The three failed tests are because the tests did not complete, I think this is nothing to do with the patch. The same thing is happening on other issues too - see #1635810: Views filtering of optional end dates #20 and #21 https://drupal.org/node/1635810#comment-7575253 and #1856306: Undefined index: repeat in date_repeat_field_bundles() #12 https://drupal.org/node/1856306#comment-7227006

Date_migrate tests fail because 'Migration' class is not found, referenced in date_migrate.test line 44

Does anyone know how to get the tests fixed? It gives a false impression that the patch has a fault.

Jonathan

theoracleprodigy’s picture

I found this works for the text format, and but does not seem to work when you use the option of popup. The popup seems to work and pick the date yet it doesn't display any content what so ever. It seems to be expecting a different format to filter the content on an exposed filter then. So I had to change to text yet the popup still shows up and works.

jonathan1055’s picture

Version: 7.x-2.6 » 7.x-2.x-dev
Status: Needs work » Needs review
FileSize
502 bytes

Re-rolled against 2.6+15. No code changed. Just wondering if the failing tests discussed in #7 and #9 have now been corrected.

jonathan1055’s picture

Title: date_popup ignores #date_format when setting the form value » Define #return_value_format for date_popup to set the form value
FileSize
791 bytes

OK, I realised that regardless of whether the original behaviour was intended or was an oversight, changing the current behaviour now will break many implementations. Hence here is a modified fix which uses a new attribute $element['#return_value_format']. If this is set the string value is returned in this format, but if it is not set the current default of DATE_FORMAT_DATETIME is used.

In date_popup_validate()

  // If the created date is valid, set it.
  $value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
  form_set_value($element, $value, $form_state);

becomes

  // If the created date is valid, set it in the form. Use the optional return
  // value format '#return_value_format' but default to DATE_FORMAT_DATETIME.
  $return_value_format = !empty($element['#return_value_format']) ? $element['#return_value_format'] : DATE_FORMAT_DATETIME;
  $value = !empty($date) ? $date->format($return_value_format) : NULL;
  form_set_value($element, $value, $form_state);

Patch against 2.6+15

Jonathan

jonathan1055’s picture

Issue summary: View changes

fixed spelling mistake

podarok’s picture

Issue summary: View changes
Status: Needs review » Postponed (maintainer needs more info)

due to #12 - is is a bug or a feature with API change?

jonathan1055’s picture

I would say that it was an oversight that was missed during development (so appears like a bug now) but it is too late to change the existing behavior because third-party modules may rely on it. Hence adding a new attribute will allow those who want to have the date returned in the matching format to get it, but the default behavior is not changed.

Jonathan

xenophyle’s picture

This is a useful patch and was necessary to get Node Expire 7.2 working.