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.

Command icon 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:

Comments

johnv created an issue. See original summary.

johnv’s picture

Issue summary: View changes
fprevos2’s picture

Any 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

fprevos2’s picture

I 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.

johnv’s picture

Title: Add generic custom time format via hook(D7/D8) » Time format: Add hook for custom format (D7/D8)
johnv’s picture

Status: Active » Needs review
afireintheattic’s picture

A 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!

splash112’s picture

Definitely 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.

capysara made their first commit to this issue’s fork.

capysara’s picture

StatusFileSize
new2.74 KB

I 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.

minoroffense’s picture

StatusFileSize
new2.75 KB

Reroll for latest release.

  • johnv committed 78f93d7 on 8.x-1.x
    Issue #3063213: Time format: Add hook for custom format
    
johnv’s picture

Version: 8.x-1.x-dev » 8.x-1.6
Category: Plan » Feature request
Issue summary: View changes
Status: Needs review » Fixed
StatusFileSize
new5.13 KB

Above 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.

  • johnv committed 8cad6f1 on 8.x-1.x
    Issue #3063213: Time format: Add hook for custom format - README.txt
    

  • johnv committed eb1d4a1 on 8.x-1.x
    Issue #3063213: Time format: Add hook for custom format
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.