You are here

function _calendar_views_track_current_date in Calendar 8

Track the current date as the user moves from calendar display to calendar display.

Parameters

\Drupal\views\ViewExecutable $view:

Return value

\Drupal\views\Plugin\views\argument\ArgumentPluginBase

1 call to _calendar_views_track_current_date()
calendar_views_pre_render in ./calendar.module
Implements hook_views_pre_render().

File

./calendar.module, line 74

Code

function _calendar_views_track_current_date(ViewExecutable $view) {
  $user = \Drupal::currentUser();
  $tracking = \Drupal::config('calendar.settings')
    ->get('calendar_track_date');
  if (!empty($tracking) && ($tracking == 2 || !empty($user->uid))) {
    foreach ($view->argument as $id => &$argument) {

      // If this is not a default date, i.e. we have browsed to a new calendar
      // period on a display we were already on, store the midpoint of the current
      // view as the current date in a session.
      if ($argument instanceof CalendarDate && empty($argument->is_default)) {
        $date_range = $argument->date_handler
          ->arg_range($argument->argument);
        $session_date = $date_range[0];
        $days = intval(($date_range[1]
          ->format('U') - $date_range[0]
          ->format('U')) / 3600 / 24 / 2);
        date_modify($session_date, "+{$days} days");
        $_SESSION[$view->name]['default_date'] = $session_date;
      }
    }
  }
}