There are several requests for a 'custom' time format in the Office Hours Formatter.
Instead of adding each of those formats, I propose to use :
D7: function hook_field_display_alter(&$display, $context)
D8: function hook_entity_view_alter(&$build, $entity, $display)
D8 introduced EntityViewDisplay config entities
In most (all) requests , a pattern can be found, which can be used in a simple string replace function call (str_replace).
So, the plan would look like:
- in the module, add an example of above hooks in office_hours.api.php, and document this somewhere.
- per custom project, add above hook and implement a str_replace
- alternatively, add an extra setting in 'field display settings' with the replace definition, Which is called by a hook that is implemented in the module.
Some examples are now documented in ofice_hours.api.php file:
// Remove separating space between time and ampm.
$formatted_time = str_replace([' am',' pm'], ['am','pm'], $formatted_time);
// Replace 'a.m.' by 'am'.
$formatted_time = str_replace(['am','pm'], ['a.m.','p.m.'], $formatted_time);
// Replace '9:00 am' by '9am'. (Separate lines to not destroy '16:00'.)
$formatted_time = str_replace([':00 a'], [' a'], $formatted_time);
$formatted_time = str_replace([':00 p'], [' p'], $formatted_time);
// Convert 'Open all day'.
// Translation can be managed on /admin/config/regional/translate.
$formatted_time = str_replace(['12a.m.-12a.m.'], ['Around the clock'], $formatted_time);
$formatted_time = str_replace(['00:00-24:00'], ['Around the clock'], $formatted_time);
$formatted_time = str_replace(['Around the clock'], ['All day open'], $formatted_time);
// Translate. Translations can be managed on /admin/config/regional/translate.
$formatted_time = t($formatted_time);
This is now implemented in v*.1-1.7
Additional feature request: in the field widget add a text(list) element, where the user can add each of the above translations, without using code.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | office_hours_3063213_14-hook_time_format.patch | 5.13 KB | johnv |
| #12 | 3063213-time-format-add-12.patch | 2.75 KB | minoroffense |
| #11 | 3063213-time-format-add-11.patch | 2.74 KB | capysara |
| #4 | office_hours-support-code-time-format-3063213-4-D8.patch | 2.79 KB | fprevos2 |
Issue fork office_hours-3063213
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3063213-time-format-add
changes, plain diff MR !6
Comments
Comment #2
johnvComment #3
fprevos2 commentedAny reason why we don't want to support the Drupal core format found at admin/config/regional/date-time instead of requesting users to create a custom hook?
Thanks
Comment #4
fprevos2 commentedI know that the patch is incomplete but it was more a proof of concept. I was planning on doing the proof of concept to support Drupal core date and time format for both the "day_format" and the "time_format" config. But I realize that the "day_format" was more complex with features like grouping days with the same hours. Another thing to be careful with is the "time_format" could technically include the date portion since I could not find an easy way filter the available format.
Comment #5
johnvComment #6
johnvComment #7
afireintheattic commentedA much needed feature! As I am not using any grouping features and just needed to be able to use custom date/time formats, this patch works perfectly. Thanks!
Comment #8
splash112 commentedDefinitely go for the Drupal time format integration. Clean and flexible.
Probably will need to keep the time and day of week separately. So far looks like it should be relatively easy. Just missing a 2 letter weekday abbreviation from PHP.
Comment #11
capysara commentedI re-rolled the patch for the current dev version and created a merge request. The only change I made from the patch in #4 is that I removed the commented out code. Attaching a patch file, too, for review.
Comment #12
minoroffense commentedReroll for latest release.
Comment #14
johnvAbove patch adds an alter_hook hook_office_hours_time_format_alter(string &$formatted_time) .
Using this hook, you can change the time format, and/or insert a translatable text, as documented in office_hours.api.php file.