diff --git a/src/Service/UWService.php b/src/Service/UWService.php index 29f37a47809206224089d9fdcf4bd00ea487d4c7..e476b8b0c22b849869442fc8cb69b6ea93b3ff13 100644 --- a/src/Service/UWService.php +++ b/src/Service/UWService.php @@ -11,6 +11,7 @@ use Drupal\Core\Url; use Drupal\node\Entity\Node; use Drupal\simplify_menu\MenuItems; use Drupal\path_alias\AliasManager; +use Symfony\Component\HttpFoundation\RequestStack; /** * Class UWService. @@ -49,6 +50,13 @@ class UWService implements UWServiceInterface { */ private $pathAliasManager; + /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + private $requestStack; + /** * Default constructor. * @@ -60,12 +68,15 @@ class UWService implements UWServiceInterface { * The simplify_menu menu items. * @param \Drupal\path_alias\AliasManager $pathAliasManager * The Drupal path alias manager. + * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack + * The request stack. */ - public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database, MenuItems $simplifyMenu, AliasManager $pathAliasManager) { + public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $database, MenuItems $simplifyMenu, AliasManager $pathAliasManager, RequestStack $requestStack) { $this->entityTypeManager = $entityTypeManager; $this->database = $database; $this->simplifyMenu = $simplifyMenu; $this->pathAliasManager = $pathAliasManager; + $this->requestStack = $requestStack; } /** @@ -607,41 +618,63 @@ class UWService implements UWServiceInterface { // Get all the dates. $dates = $node->$field_name->getValue(); + // Get the date query parameter. + $date_parameter = $this->requestStack->getCurrentRequest()->query->get('date'); + + // If there is a date query parameter, convert + // to timestamp so we can compare against dates + // in the event. If there is no parameter, set + // the timestamp todays date. + if ($date_parameter) { + $check_date = strtotime($date_parameter['value']); + } + else { + $check_date = strtotime("now"); + } + // Step through each of the dates and get // out correct values. foreach ($dates as $date) { - // The all day case, duration is always 1439. - if ($date['duration'] == '1439' && $date['end_value'] > strtotime("now")) { - $return_dates[] = date('l, F j, Y', $date['value']) . ' (all day)'; + // Ensure that the dates are greater than timestamp + // that we generated above. + if ($date['end_value'] > $check_date) { + $return_dates[] = $this->UwGetDate($date, 'event'); } - else { + } + } - // If the date is upcoming, meaning greater than right now. - // Taking this out for now, will be putting it back, if - // we figure out how to do this in the view. - // if ($date['end_value'] > strtotime("today")) {. - // If this is the same day, get the date and the start - // and end times. - if ($date['duration'] < '1439') { - $start_date = date('l, F j, Y g:i A', $date['value']); - $end_date = date('g:i A', $date['end_value']); - } + return $return_dates; + } - // This is not the day, get the start and end date with time. - else { - $start_date = date('l, F j, Y g:i A', $date['value']); - $end_date = date('l, F j, Y g:i A', $date['end_value']); - } + /** + * {@inheritDoc} + */ + public function uwGetDate(array $date, string $type): string { - // Add the start and end date with timezone. - $return_dates[] = $start_date . ' - ' . $end_date . ' ' . date('T', $date['end_value']); - // } - } + // The all day case, duration is always 1439. + if ($date['duration'] == '1439') { + $return_date = date('l, F j, Y', $date['value']) . ' (all day)'; + } + else { + + // If this is the same day, get the date and the start + // and end times. + if ($date['duration'] < '1439') { + $start_date = date('l, F j, Y g:i A', $date['value']); + $end_date = date('g:i A', $date['end_value']); + } + // This is not the day, get the start and end date with time. + else { + $start_date = date('l, F j, Y g:i A', $date['value']); + $end_date = date('l, F j, Y g:i A', $date['end_value']); } + + // Add the start and end date with timezone. + $return_date = $start_date . ' - ' . $end_date . ' ' . date('T', $date['end_value']); } - return $return_dates; + return $return_date; } /** diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php index 3bdcc8efc0ed4b87624db4aba4636f1c301be864..5375067ad5f119d9055841d13c9f0d73755700a0 100644 --- a/src/Service/UWServiceInterface.php +++ b/src/Service/UWServiceInterface.php @@ -93,6 +93,19 @@ interface UWServiceInterface { */ public function uwGetDates(Node $node, string $field_name): array; + /** + * Get a date in the proper format. + * + * @param array $date + * An array of date info. + * @param string $type + * The type of date. + * + * @return string + * A converted date to a string. + */ + public function uwGetDate(array $date, string $type): string; + /** * Gets image from node. * diff --git a/uw_cfg_common.services.yml b/uw_cfg_common.services.yml index d577a012687f929c10b7a0159eb80f13b4240028..d98dafb91c967ea1f5b5fce5a745e38ff0418093 100644 --- a/uw_cfg_common.services.yml +++ b/uw_cfg_common.services.yml @@ -4,7 +4,7 @@ services: arguments: ['@config.factory', '@current_user'] uw_cfg_common.uw_service: class: Drupal\uw_cfg_common\Service\UWService - arguments: ['@entity_type.manager', '@database', '@simplify_menu.menu_items', '@path_alias.manager'] + arguments: ['@entity_type.manager', '@database', '@simplify_menu.menu_items', '@path_alias.manager', '@request_stack'] uw_cfg_common.route_subscriber: class: Drupal\uw_cfg_common\Routing\UwNodeAccessRouteSubscriber tags: