Hello,

I am liking the module so far and I would like to use it on a D8 site. Are there any plans to include the option to set the start and/or end time to midnight? I saw a similar issue was resolved for D7 but nothing for D8: https://www.drupal.org/node/1661512

Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tklepsch created an issue. See original summary.

heikki’s picture

I ran into this same issue myself.
I made a patch that fixed the issue for me.
The patch makes saving hour value with 0 to 24 (for example 0:30 (030 -> 2430)).
The patch isn't tested with all the possible setting combinations, but at least works fine with 24h -format and without the "Validate hours" option.

The proper way to fix would be to change the fields to varchar, to enable values with starting zeroes.
As the issue with saving midnight comes from values being '000' but the DB field being of 'INT' type (defined in 'office_hours/src/Plugin/Field/FieldType/OfficeHoursItem.php').
^^ This is also reported here: https://www.drupal.org/node/2768577

heikki’s picture

Status: Active » Needs review
tklepsch’s picture

The patch seems to work as advertised and I am able to display it in either 12h or 24h formats within nodes and views. However, when saving the content, I am presented with the following warning:

Warning: strlen() expects parameter 1 to be string, array given in Drupal\office_hours\OfficeHoursDateHelper::convertSafeTime() (line 152 of sites/default/modules/office_hours/src/OfficeHoursDateHelper.php).
Drupal\office_hours\OfficeHoursDateHelper::convertSafeTime(Array, ) (Line: 83)
Drupal\office_hours\Element\OfficeHoursSelect::valueCallback(Array, Array, Object)
call_user_func_array(Array, Array) (Line: 1236)
Drupal\Core\Form\FormBuilder->handleInputElement('node_boutique_edit_form', Array, Object) (Line: 972)
Drupal\Core\Form\FormBuilder->doBuildForm('node_boutique_edit_form', Array, Object) (Line: 1042)
Drupal\Core\Form\FormBuilder->doBuildForm('node_boutique_edit_form', Array, Object) (Line: 1042)
Drupal\Core\Form\FormBuilder->doBuildForm('node_boutique_edit_form', Array, Object) (Line: 1042)
Drupal\Core\Form\FormBuilder->doBuildForm('node_boutique_edit_form', Array, Object) (Line: 1042)
Drupal\Core\Form\FormBuilder->doBuildForm('node_boutique_edit_form', Array, Object) (Line: 1042)
Drupal\Core\Form\FormBuilder->doBuildForm('node_boutique_edit_form', Array, Object) (Line: 555)
Drupal\Core\Form\FormBuilder->processForm('node_boutique_edit_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 139)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

This repeats about 28 times (which correlates to the number of values available). I'm not seeing any adverse affects as a result of the warning and the information is still saved to the database.

heikki’s picture

Thanks for testing tklepsch!

I updated the patch to prevent these warnings.
Also improved returning of the 24xx values in format() helper (I wasn't able to alter those times in my custom field formatter before).

johnv’s picture

You can move $time = substr('0000' . $time, -4) into convertSafeTim(). Then strlen is always 4.

johnv’s picture

ConvertSafeTime takes a second parameter as Boolean, but the default value is 1, not TRUE. You can add the type in the variables declaration and documentation, too.

johnv’s picture

Better name parameter $to as $direction. The current name sounds like from-to , not like vice-versa.

johnv’s picture

Thanks for the patch. Sorry for the little nitbits. I do this on my smartphone on holidays.

johnv’s picture

Category: Task » Bug report
Status: Needs review » Needs work
Shreya Shetty’s picture

Thank you for the review . I have made some changes according to your suggestions

Shreya Shetty’s picture

Status: Needs work » Needs review
heikki’s picture

I don't have any more time to use on this issue, so it would be much appreciated if someone could iron the minor kinks out.
Like johnv said, it would be good idea to include the $time = substr('0000' . $time, -4) in convertSafeTime().

Shreya Shetty’s picture

tklepsch’s picture

I can confirm that both patch #5 and patch #11 do not display the warning label and work as desired without issue. I can't really give much feedback regarding the code, as it's not my area of expertise.

In patch #13, However, when I save the content the values with 0:00 are reset to the default settings of Hour: Minutes and those values are no longer displayed in the published nodes or views. No warnings or errors are shown in the process.

johnv’s picture

Component: Code » Code - widget
johnv’s picture

Todays commits from other issues already solve the problem if one of both hours is midnight.
I have the impression that the new function OfficeHoursDateHelper::convertSafeTime() duplicates OfficeHoursDateHelper::format()
I will first work on #2808909: Time Format: ampm with periods (a.m. and p.m.), where all hours will be formatted using 1 function OfficeHoursDateHelper::format().

johnv’s picture

Title: Setting opening and closing hours to Midnight » Setting opening OR closing hours to Midnight
Category: Bug report » Feature request
Related issues: +#2860255: Feature: around the clock (D8)

OK, #2808909 is done for now.
ITMT, #2860255: Feature: around the clock (D8) is raised.

Let's close this issue, since the OR case is resolved. And handle the AND case in #2860255: Feature: around the clock (D8)

johnv’s picture

Version: 8.x-1.x-dev » 8.x-1.0-alpha1
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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