MODX Community

Newspublisher: use native browser datepicker for date-time variable / parser problems?


For the sake of consistency I’d like to use the browser-native date- and time pickers if possible. So I duplicated and renamed npDateTpl, removed all the JS and changed the type to date and time, resp.:

    <div id="np-[[+npx.fieldName]]-container" class="np-date">
      <label class="fieldlabel" for="[[+npx.fieldName]]" title="[[]]">[[+npx.caption]]:</label>
      <div class = "np-date-hints"><span class = "np-date-hint"> [[%np_date_hint]]</span><span class ="np-time-hint">[[%np_time_hint]]</span></div>
      <input type="date" id="[[+npx.fieldName]]" name="[[+npx.fieldName]]" maxlength="10" value="[[+np.[[+npx.fieldName]]]]" />
      <input type="time" class="[[+npx.fieldName]]_time" name="[[+npx.fieldName]]_time" id="[[+npx.fieldName]]_time" maxlength="10" value="[[+np.[[+npx.fieldName]]_time]]" />
      <div class="invalid_date" id="[[+npx.fieldName]]_date_error" style="visibility:hidden">[[%np_invalid_date]]</div>

This works for storing time and date in the TV, but the fields are not filled out correctly afterwards when someone wants to review or change the data. The reason is the format the date is returned by using
[[+np.[[+npx.fieldName]]]]. This returns YYYYMMDD, whereas the type=date input needs YYYY-MM-DD

So I thought I could use a quick and dirty output modifier and insert the dashes with that:

$output = substr($input,0,4)."-".substr($input,4,2)."-".substr($input,6,2);
return $output;

This works when the name of the date-time-TV is used explicitly:

But not when used like this:

in fact the MODX parser returns the value of a totally different TV. I also tried all combinations of calling the placeholders cached: no success.

So is this a bug in the MODX parser/caching, in Newspublisher or (most likely) in my brain?


1 Like

I don’t know why you would get a different TV, but you might try this. Around line 1664 of this file:


Change this line:

$format = str_replace( array('-','sp','dt','sl','ds','cc'),
                 array( '', ' ', '.', '/', '-', ','), $format);

to this:

$format = str_replace( array('-','sp','dt','sl','ds','cc'),
                 array( '-', ' ', '.', '/', '-', ','), $format);

(changing the first element of the second array to a dash should prevent the dashes from being removed).

If it works, you’d have to redo the change if you update NewsPublisher.

1 Like