Loading src/OfficeHoursDateHelper.php +24 −13 Original line number Diff line number Diff line Loading @@ -298,17 +298,27 @@ class OfficeHoursDateHelper extends DateHelper { * {@inheritdoc} */ public static function weekDaysOrdered($office_hours, $first_day = NULL) { $new_office_hours = []; // Do an initial re-sort on day number. ksort($office_hours); // Fetch first day of week from field settings. // $first_day = \Drupal::config('system.date')->get('first_day'); $first_day = $first_day ?? OfficeHoursDateHelper::getFirstDay(); // Sort Weekdays; leave Exception days at bottom of list. foreach ($office_hours as $key => $item) { $new_office_hours[$key - (OfficeHoursDateHelper::DAYS_PER_WEEK * (int) ($key >= $first_day))] = $item; if ($first_day > 0) { // Sort Weekdays. Leave Exception days at bottom of list. // Copying to new array to preserve keys. for ($i = $first_day; $i <= OfficeHoursDateHelper::DAYS_PER_WEEK; $i++) { // Rescue the element to be moved. if (isset($office_hours[$i])) { $new_office_hours[$i] = $office_hours[$i]; // Remove this week day from the old array. unset($office_hours[$i]); } } // @todo Use pattern from extractFormValues(), avoiding key-change. // usort($values, function ($a, $b) { // return SortArray::sortByKeyInt($a, $b, '_weight'); // } ksort($new_office_hours); return $new_office_hours; } return $new_office_hours + $office_hours; } /** Loading Loading @@ -418,13 +428,14 @@ class OfficeHoursDateHelper extends DateHelper { /** * Returns whether the day number is a weekday or a date (Exception day). * * @param int $day_number * Weekday (using date_api as key (0=Sun, 6=Sat)) or Exception day date. * @param array $value * The Office hours data structure, having 'day' as weekday * (using date_api as key (0=Sun, 6=Sat)) or Exception day date. * * @return bool * False if the day_number is a (sorted) weekday (-7 until +7). * False if the day_number is a (sorted) weekday (0 to 7). */ public static function isExceptionDay($value) { public static function isExceptionDay(array $value) { $day = $value['startday'] ?? $value['day']; // Grouped days have 'startday'. return (8 <= (int) $day); } Loading src/OfficeHoursFormatterTrait.php +14 −12 Original line number Diff line number Diff line Loading @@ -3,8 +3,6 @@ namespace Drupal\office_hours; use Drupal\Component\Utility\Html; use Drupal\office_hours\Element\OfficeHoursBaseSlot; use Drupal\office_hours\Element\OfficeHoursDatetime; /** * Factors out OfficeHoursItemList->getItems()->getRows(). Loading @@ -21,13 +19,16 @@ trait OfficeHoursFormatterTrait { * @param array $values * Result from FieldItemListInterface $items->getValue(). * @param array $settings * The settings. * @param array $field_settings * The field settings. * @param $time * A time stamp. Defaults to 'REQUEST_TIME'. * * @return array * The formatted list of slots. */ public function getRows(array $values, array $settings, array $field_settings, $time = NULL) { public function getRows(array $values, array $settings, array $field_settings, array $third_party_settings = [], $time = NULL) { $default_office_hours = [ 'startday' => NULL, Loading Loading @@ -130,7 +131,7 @@ trait OfficeHoursFormatterTrait { // From here, no more adding/removing, only formatting. // Format the day names. $office_hours = $this->formatLabels($office_hours, $settings); $office_hours = $this->formatLabels($office_hours, $settings, $third_party_settings); // Format the start and end time into one slot. $office_hours = $this->formatSlots($office_hours, $settings, $field_settings); Loading Loading @@ -194,7 +195,7 @@ trait OfficeHoursFormatterTrait { * Reformatted office hours array. */ protected function groupDays(array $office_hours) { // Keys -7 to +7 are for sorted weekdays. // Keys 0-7 are for sorted Weekdays. $previous_key = -100; $previous_day = ['slots' => 'dummy']; Loading Loading @@ -259,7 +260,7 @@ trait OfficeHoursFormatterTrait { * * @param array $office_hours * Office hours array. * @param $time * @param int|mixed $time * A time stamp. Defaults to 'REQUEST_TIME'. * * @return array Loading @@ -284,7 +285,7 @@ trait OfficeHoursFormatterTrait { * * @param array $office_hours * Office hours array. * @param $time * @param int|mixed $time * A time stamp. Defaults to 'REQUEST_TIME'. * * @return array Loading Loading @@ -340,14 +341,15 @@ trait OfficeHoursFormatterTrait { * Office hours array. * @param array $settings * User settings array. * @param array $third_party_settings * Formatter third party settings array. * * @return array * Reformatted office hours array. */ protected function formatLabels(array $office_hours, array $settings) { protected function formatLabels(array $office_hours, array $settings, array $third_party_settings) { $day_format = $settings['day_format']; $exceptions_day_format = $settings['exceptions']['date_format'] ?? NULL; $exceptions_day_format = $third_party_settings['office_hours_exceptions']['date_format'] ?? NULL; $group_separator = $settings['separator']['grouped_days']; $days_suffix = $settings['separator']['day_hours']; foreach ($office_hours as $key => &$info) { Loading src/Plugin/Field/FieldFormatter/OfficeHoursFormatterDefault.php +2 −1 Original line number Diff line number Diff line Loading @@ -38,10 +38,11 @@ class OfficeHoursFormatterDefault extends OfficeHoursFormatterBase { } $settings = $this->getSettings(); $third_party_settings = $this->getThirdPartySettings(); $field_definition = $items->getFieldDefinition(); // N.B. 'Show current day' may return nothing in getRows(), while other days are filled. /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */ $office_hours = $items->getRows($settings, $this->getFieldSettings()); $office_hours = $items->getRows($settings, $this->getFieldSettings(), $third_party_settings); $elements[] = [ '#theme' => 'office_hours', '#parent' => $field_definition, Loading src/Plugin/Field/FieldFormatter/OfficeHoursFormatterSchema.php +2 −1 Original line number Diff line number Diff line Loading @@ -74,9 +74,10 @@ class OfficeHoursFormatterSchema extends OfficeHoursFormatterBase { // Get some settings from field. Do not overwrite defaults. $settings = $this->defaultSettings() + $this->getSettings(); $third_party_settings = $this->getThirdPartySettings(); // N.B. 'Show current day' may return nothing in getRows(), while other days are filled. /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */ $office_hours = $items->getRows($settings, $this->getFieldSettings()); $office_hours = $items->getRows($settings, $this->getFieldSettings(), $third_party_settings); $elements[] = [ '#theme' => 'office_hours_schema', // Pass office_hours to twig theming. Loading src/Plugin/Field/FieldFormatter/OfficeHoursFormatterTable.php +2 −1 Original line number Diff line number Diff line Loading @@ -37,10 +37,11 @@ class OfficeHoursFormatterTable extends OfficeHoursFormatterBase { } $settings = $this->getSettings(); $third_party_settings = $this->getThirdPartySettings(); $field_definition = $items->getFieldDefinition(); // N.B. 'Show current day' may return nothing in getRows(), while other days are filled. /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */ $office_hours = $items->getRows($settings, $this->getFieldSettings()); $office_hours = $items->getRows($settings, $this->getFieldSettings(), $third_party_settings); // For a11y screen readers, a header is introduced. // Superfluous comments are removed. @see #3110755 for examples and explanation. Loading Loading
src/OfficeHoursDateHelper.php +24 −13 Original line number Diff line number Diff line Loading @@ -298,17 +298,27 @@ class OfficeHoursDateHelper extends DateHelper { * {@inheritdoc} */ public static function weekDaysOrdered($office_hours, $first_day = NULL) { $new_office_hours = []; // Do an initial re-sort on day number. ksort($office_hours); // Fetch first day of week from field settings. // $first_day = \Drupal::config('system.date')->get('first_day'); $first_day = $first_day ?? OfficeHoursDateHelper::getFirstDay(); // Sort Weekdays; leave Exception days at bottom of list. foreach ($office_hours as $key => $item) { $new_office_hours[$key - (OfficeHoursDateHelper::DAYS_PER_WEEK * (int) ($key >= $first_day))] = $item; if ($first_day > 0) { // Sort Weekdays. Leave Exception days at bottom of list. // Copying to new array to preserve keys. for ($i = $first_day; $i <= OfficeHoursDateHelper::DAYS_PER_WEEK; $i++) { // Rescue the element to be moved. if (isset($office_hours[$i])) { $new_office_hours[$i] = $office_hours[$i]; // Remove this week day from the old array. unset($office_hours[$i]); } } // @todo Use pattern from extractFormValues(), avoiding key-change. // usort($values, function ($a, $b) { // return SortArray::sortByKeyInt($a, $b, '_weight'); // } ksort($new_office_hours); return $new_office_hours; } return $new_office_hours + $office_hours; } /** Loading Loading @@ -418,13 +428,14 @@ class OfficeHoursDateHelper extends DateHelper { /** * Returns whether the day number is a weekday or a date (Exception day). * * @param int $day_number * Weekday (using date_api as key (0=Sun, 6=Sat)) or Exception day date. * @param array $value * The Office hours data structure, having 'day' as weekday * (using date_api as key (0=Sun, 6=Sat)) or Exception day date. * * @return bool * False if the day_number is a (sorted) weekday (-7 until +7). * False if the day_number is a (sorted) weekday (0 to 7). */ public static function isExceptionDay($value) { public static function isExceptionDay(array $value) { $day = $value['startday'] ?? $value['day']; // Grouped days have 'startday'. return (8 <= (int) $day); } Loading
src/OfficeHoursFormatterTrait.php +14 −12 Original line number Diff line number Diff line Loading @@ -3,8 +3,6 @@ namespace Drupal\office_hours; use Drupal\Component\Utility\Html; use Drupal\office_hours\Element\OfficeHoursBaseSlot; use Drupal\office_hours\Element\OfficeHoursDatetime; /** * Factors out OfficeHoursItemList->getItems()->getRows(). Loading @@ -21,13 +19,16 @@ trait OfficeHoursFormatterTrait { * @param array $values * Result from FieldItemListInterface $items->getValue(). * @param array $settings * The settings. * @param array $field_settings * The field settings. * @param $time * A time stamp. Defaults to 'REQUEST_TIME'. * * @return array * The formatted list of slots. */ public function getRows(array $values, array $settings, array $field_settings, $time = NULL) { public function getRows(array $values, array $settings, array $field_settings, array $third_party_settings = [], $time = NULL) { $default_office_hours = [ 'startday' => NULL, Loading Loading @@ -130,7 +131,7 @@ trait OfficeHoursFormatterTrait { // From here, no more adding/removing, only formatting. // Format the day names. $office_hours = $this->formatLabels($office_hours, $settings); $office_hours = $this->formatLabels($office_hours, $settings, $third_party_settings); // Format the start and end time into one slot. $office_hours = $this->formatSlots($office_hours, $settings, $field_settings); Loading Loading @@ -194,7 +195,7 @@ trait OfficeHoursFormatterTrait { * Reformatted office hours array. */ protected function groupDays(array $office_hours) { // Keys -7 to +7 are for sorted weekdays. // Keys 0-7 are for sorted Weekdays. $previous_key = -100; $previous_day = ['slots' => 'dummy']; Loading Loading @@ -259,7 +260,7 @@ trait OfficeHoursFormatterTrait { * * @param array $office_hours * Office hours array. * @param $time * @param int|mixed $time * A time stamp. Defaults to 'REQUEST_TIME'. * * @return array Loading @@ -284,7 +285,7 @@ trait OfficeHoursFormatterTrait { * * @param array $office_hours * Office hours array. * @param $time * @param int|mixed $time * A time stamp. Defaults to 'REQUEST_TIME'. * * @return array Loading Loading @@ -340,14 +341,15 @@ trait OfficeHoursFormatterTrait { * Office hours array. * @param array $settings * User settings array. * @param array $third_party_settings * Formatter third party settings array. * * @return array * Reformatted office hours array. */ protected function formatLabels(array $office_hours, array $settings) { protected function formatLabels(array $office_hours, array $settings, array $third_party_settings) { $day_format = $settings['day_format']; $exceptions_day_format = $settings['exceptions']['date_format'] ?? NULL; $exceptions_day_format = $third_party_settings['office_hours_exceptions']['date_format'] ?? NULL; $group_separator = $settings['separator']['grouped_days']; $days_suffix = $settings['separator']['day_hours']; foreach ($office_hours as $key => &$info) { Loading
src/Plugin/Field/FieldFormatter/OfficeHoursFormatterDefault.php +2 −1 Original line number Diff line number Diff line Loading @@ -38,10 +38,11 @@ class OfficeHoursFormatterDefault extends OfficeHoursFormatterBase { } $settings = $this->getSettings(); $third_party_settings = $this->getThirdPartySettings(); $field_definition = $items->getFieldDefinition(); // N.B. 'Show current day' may return nothing in getRows(), while other days are filled. /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */ $office_hours = $items->getRows($settings, $this->getFieldSettings()); $office_hours = $items->getRows($settings, $this->getFieldSettings(), $third_party_settings); $elements[] = [ '#theme' => 'office_hours', '#parent' => $field_definition, Loading
src/Plugin/Field/FieldFormatter/OfficeHoursFormatterSchema.php +2 −1 Original line number Diff line number Diff line Loading @@ -74,9 +74,10 @@ class OfficeHoursFormatterSchema extends OfficeHoursFormatterBase { // Get some settings from field. Do not overwrite defaults. $settings = $this->defaultSettings() + $this->getSettings(); $third_party_settings = $this->getThirdPartySettings(); // N.B. 'Show current day' may return nothing in getRows(), while other days are filled. /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */ $office_hours = $items->getRows($settings, $this->getFieldSettings()); $office_hours = $items->getRows($settings, $this->getFieldSettings(), $third_party_settings); $elements[] = [ '#theme' => 'office_hours_schema', // Pass office_hours to twig theming. Loading
src/Plugin/Field/FieldFormatter/OfficeHoursFormatterTable.php +2 −1 Original line number Diff line number Diff line Loading @@ -37,10 +37,11 @@ class OfficeHoursFormatterTable extends OfficeHoursFormatterBase { } $settings = $this->getSettings(); $third_party_settings = $this->getThirdPartySettings(); $field_definition = $items->getFieldDefinition(); // N.B. 'Show current day' may return nothing in getRows(), while other days are filled. /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */ $office_hours = $items->getRows($settings, $this->getFieldSettings()); $office_hours = $items->getRows($settings, $this->getFieldSettings(), $third_party_settings); // For a11y screen readers, a header is introduced. // Superfluous comments are removed. @see #3110755 for examples and explanation. Loading