Loading office_hours.api.php 0 → 100644 +41 −0 Original line number Diff line number Diff line <?php /** * @file * Hooks and API provided by the "Office Hours" module. */ use Drupal\Core\Entity\EntityInterface; /** * @addtogroup hooks * @{ */ /** * Allows to alter the current time. * * @param int $time * A Unix timestamp. * @param \Drupal\Core\Entity\EntityInterface $entity * * @see issue [#1925272] Handle timezone. */ function hook_office_hours_current_time_alter(int &$time, EntityInterface $entity) { // Add user timezone to time. // Assume that all owned entities (for example, offices) should // depend on user timezone. if ($entity instanceof \Drupal\user\EntityOwnerInterface && ($account = $entity->getOwner()) && ($account->isAuthenticated()) ) { $date_time = new \DateTime(); $date_time->setTimestamp($time); $date_time->setTimezone(new \DateTimeZone($account->getTimeZone())); $time = $date_time->getTimestamp(); } } /** * @} End of "addtogroup hooks". */ src/OfficeHoursFormatterTrait.php +9 −4 Original line number Diff line number Diff line Loading @@ -225,9 +225,13 @@ trait OfficeHoursFormatterTrait { protected function keepCurrentDay(array $office_hours, $time = NULL) { $result = []; $time = $time ?? \Drupal::time()->getRequestTime(); // Get the current time. May be adapted for User Timezone. $time = $this->getRequestTime($time); // Convert day number to integer to get '0' for Sunday, not 'false'. $today = (int) idate('w', $time); // Get day_number (0=Sun, 6=Sat). // Loop through all items. // Keep the current one. foreach ($office_hours as $info) { if ($info['startday'] == $today) { $result[$today] = $info; Loading @@ -252,13 +256,14 @@ trait OfficeHoursFormatterTrait { protected function keepCurrentSlot(array $office_hours, $time = NULL) { $result = []; // Loop through all lines. // Detect the current line and the open/closed status. // Get the current time. May be adapted for User Timezone. $time = $this->getRequestTime($time); // Convert day number to integer to get '0' for Sunday, not 'false'. $time = $time ?? \Drupal::time()->getRequestTime(); $today = (int) idate('w', $time); // Get day_number (0=Sun, 6=Sat). $now = date('Hi', $time); // 'Hi' format, with leading zero (0900). // Loop through all items. // Detect the current item and the open/closed status. foreach ($office_hours as $key => $info) { // Calculate start and end times. $day = (int) $info['day']; Loading src/Plugin/Field/FieldType/OfficeHoursItemList.php +20 −3 Original line number Diff line number Diff line Loading @@ -73,11 +73,28 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn */ public function getRows(array $settings, array $field_settings, array $third_party_settings, $time = NULL) { // @todo Move more from getRows here, using itemList, not values. $this->getCurrentSlot(); $this->getCurrentSlot($time); $this->keepExceptionDaysInHorizon($settings['exceptions']['restrict_exceptions_to_num_days'] ?? 0); return $this->getFieldRows($this->getValue(), $settings, $field_settings, $third_party_settings, $time); } /** * Returns the timestamp for the current request. * * @return int * A Unix timestamp. * * @see \Drupal\Component\Datetime\TimeInterface */ public function getRequestTime($time) { $time = ($time) ?? \Drupal::time()->getRequestTime(); // Call hook. Allows to alter the current time using a timezone. $entity = $this->getEntity(); \Drupal::moduleHandler()->alter('office_hours_current_time', $time, $entity); return $time; } /** * Get the current slot and the next day from the Office hours. * Loading @@ -94,7 +111,7 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn } // Detect the current slot and the open/closed status. $time = ($time) ?? \Drupal::time()->getRequestTime(); $time = $this->getRequestTime($time); $today = (int) idate('w', $time); // Get day_number: (0=Sun, 6=Sat). $now = date('Hi', $time); // 'Hi' format, with leading zero (0900). Loading Loading @@ -165,7 +182,7 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn /** * {@inheritdoc} */ public function getCacheTime(array $settings, array $field_settings) { public function getCacheTime(array $settings, array $field_settings, array $third_party_settings) { // @see https://www.drupal.org/docs/drupal-apis/cache-api/cache-max-age // If there are no open days, cache forever. Loading src/Plugin/Field/FieldType/OfficeHoursItemListInterface.php +3 −1 Original line number Diff line number Diff line Loading @@ -40,13 +40,15 @@ interface OfficeHoursItemListInterface extends FieldItemListInterface { * The formatter settings. * @param array $field_settings * The field settings. * @param array $third_party_settings * The formatter's third party settings. * * @return int * The time that a render element (formatter) can be cached. * * @see https://www.drupal.org/docs/8/api/cache-api/cache-max-age */ public function getCacheTime(array $settings, array $field_settings); public function getCacheTime(array $settings, array $field_settings, array $third_party_settings); /** * Determines if the Entity has Exception days. Loading Loading
office_hours.api.php 0 → 100644 +41 −0 Original line number Diff line number Diff line <?php /** * @file * Hooks and API provided by the "Office Hours" module. */ use Drupal\Core\Entity\EntityInterface; /** * @addtogroup hooks * @{ */ /** * Allows to alter the current time. * * @param int $time * A Unix timestamp. * @param \Drupal\Core\Entity\EntityInterface $entity * * @see issue [#1925272] Handle timezone. */ function hook_office_hours_current_time_alter(int &$time, EntityInterface $entity) { // Add user timezone to time. // Assume that all owned entities (for example, offices) should // depend on user timezone. if ($entity instanceof \Drupal\user\EntityOwnerInterface && ($account = $entity->getOwner()) && ($account->isAuthenticated()) ) { $date_time = new \DateTime(); $date_time->setTimestamp($time); $date_time->setTimezone(new \DateTimeZone($account->getTimeZone())); $time = $date_time->getTimestamp(); } } /** * @} End of "addtogroup hooks". */
src/OfficeHoursFormatterTrait.php +9 −4 Original line number Diff line number Diff line Loading @@ -225,9 +225,13 @@ trait OfficeHoursFormatterTrait { protected function keepCurrentDay(array $office_hours, $time = NULL) { $result = []; $time = $time ?? \Drupal::time()->getRequestTime(); // Get the current time. May be adapted for User Timezone. $time = $this->getRequestTime($time); // Convert day number to integer to get '0' for Sunday, not 'false'. $today = (int) idate('w', $time); // Get day_number (0=Sun, 6=Sat). // Loop through all items. // Keep the current one. foreach ($office_hours as $info) { if ($info['startday'] == $today) { $result[$today] = $info; Loading @@ -252,13 +256,14 @@ trait OfficeHoursFormatterTrait { protected function keepCurrentSlot(array $office_hours, $time = NULL) { $result = []; // Loop through all lines. // Detect the current line and the open/closed status. // Get the current time. May be adapted for User Timezone. $time = $this->getRequestTime($time); // Convert day number to integer to get '0' for Sunday, not 'false'. $time = $time ?? \Drupal::time()->getRequestTime(); $today = (int) idate('w', $time); // Get day_number (0=Sun, 6=Sat). $now = date('Hi', $time); // 'Hi' format, with leading zero (0900). // Loop through all items. // Detect the current item and the open/closed status. foreach ($office_hours as $key => $info) { // Calculate start and end times. $day = (int) $info['day']; Loading
src/Plugin/Field/FieldType/OfficeHoursItemList.php +20 −3 Original line number Diff line number Diff line Loading @@ -73,11 +73,28 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn */ public function getRows(array $settings, array $field_settings, array $third_party_settings, $time = NULL) { // @todo Move more from getRows here, using itemList, not values. $this->getCurrentSlot(); $this->getCurrentSlot($time); $this->keepExceptionDaysInHorizon($settings['exceptions']['restrict_exceptions_to_num_days'] ?? 0); return $this->getFieldRows($this->getValue(), $settings, $field_settings, $third_party_settings, $time); } /** * Returns the timestamp for the current request. * * @return int * A Unix timestamp. * * @see \Drupal\Component\Datetime\TimeInterface */ public function getRequestTime($time) { $time = ($time) ?? \Drupal::time()->getRequestTime(); // Call hook. Allows to alter the current time using a timezone. $entity = $this->getEntity(); \Drupal::moduleHandler()->alter('office_hours_current_time', $time, $entity); return $time; } /** * Get the current slot and the next day from the Office hours. * Loading @@ -94,7 +111,7 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn } // Detect the current slot and the open/closed status. $time = ($time) ?? \Drupal::time()->getRequestTime(); $time = $this->getRequestTime($time); $today = (int) idate('w', $time); // Get day_number: (0=Sun, 6=Sat). $now = date('Hi', $time); // 'Hi' format, with leading zero (0900). Loading Loading @@ -165,7 +182,7 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn /** * {@inheritdoc} */ public function getCacheTime(array $settings, array $field_settings) { public function getCacheTime(array $settings, array $field_settings, array $third_party_settings) { // @see https://www.drupal.org/docs/drupal-apis/cache-api/cache-max-age // If there are no open days, cache forever. Loading
src/Plugin/Field/FieldType/OfficeHoursItemListInterface.php +3 −1 Original line number Diff line number Diff line Loading @@ -40,13 +40,15 @@ interface OfficeHoursItemListInterface extends FieldItemListInterface { * The formatter settings. * @param array $field_settings * The field settings. * @param array $third_party_settings * The formatter's third party settings. * * @return int * The time that a render element (formatter) can be cached. * * @see https://www.drupal.org/docs/8/api/cache-api/cache-max-age */ public function getCacheTime(array $settings, array $field_settings); public function getCacheTime(array $settings, array $field_settings, array $third_party_settings); /** * Determines if the Entity has Exception days. Loading