signalpoint / date

The DrupalGap Date module.
0 stars 6 forks source link

Add 12 Hour Support #8

Closed signalpoint closed 8 years ago

signalpoint commented 9 years ago

Right now if you try to create a node with a date field that is configured for 12 hour format, this module tries to POST a 24 hour format hour, which results in "an illegal choice has been detected". To get around this, you can implement something like this in a custom module:

/**
 * Implements hook_services_preprocess().
 */
function my_module_services_preprocess(options) {
  try {
    // When creating a party or event, we need to convert the hour to 1-12 and
    // add the 'ampm' value because of a bug in the Date module.
    if (options.service == 'node' && options.resource == 'create') {
      var data = JSON.parse(options.data);
      var date = data.field_start_date_and_time['und'][0];
      if (date.value.hour >= 12) {
        date.value.hour = date.value.hour % 12;
        date.value.ampm = 'pm';
      }
      if (date.value2.hour >= 12) {
        date.value2.hour = date.value2.hour % 12;
        date.value2.ampm = 'pm';
      }
      options.data = JSON.stringify(data);
    }
  }
  catch (error) { console.log('my_module_services_preprocess - ' + error); }
}
mkinnan commented 8 years ago

Is this still valid? I am still getting errors ... I did change options.bundle to my content type.

date-error

mkinnan commented 8 years ago

And is this code snippet supposed to also convert the node edit form to 12 hour format too with AM/PM? All the settings I change always show 24 hour format.

date-edit

mkinnan commented 8 years ago

Using console.log(options) is only showing the following. There is no options.resource == create or options.bundle == something from what I can find. Has something changed with hook_services_preprocess()?

retrieve

signalpoint commented 8 years ago

@mkinnan During preprocess, the bundle isn't yet known because it hasn't retrieved the node from the server yet, so it only knows the entity type (node) until we get back the fully loaded node, then it looks to the type and stick it into bundle so it's available in post processors. I see now that I suggested that was possible in my code above, but that is wrong and I'll update that post shortly.

Here's the spot in date.js that needs some love in order to get 12 hour support working: https://github.com/signalpoint/date/blob/7.x-1.x/date.js#L444

signalpoint commented 8 years ago

@mkinnan But during that preprocess you should have access to the node basics whether or not you're creating or editing a node, so I bet the bundle (aka node type) is hidden in the options somewhere so you could limit it to certain content type(s).

mkinnan commented 8 years ago

@signalpoint I spent a while trying to find the bundle type in the options but couldn't find it. I clicked through the entire options object but didn't see a bundle or node type. I'll try looking again.

signalpoint commented 8 years ago

We now have 12/24 hour support. However depending on the date's widget, we may have mixed results. I've tested pretty thoroughly for the "Date" select list widgets.

mkinnan commented 8 years ago

Date select widgets work for 12 hour format.

12 hour format is not working for the display of the date/time when viewing the node. 24 hour format is still being displayed.