You are here

public function FullCalendar::preView in FullCalendar 8.3

Same name and namespace in other branches
  1. 8.5 src/Plugin/fullcalendar/type/FullCalendar.php \Drupal\fullcalendar\Plugin\fullcalendar\type\FullCalendar::preView()
  2. 8 src/Plugin/fullcalendar/type/FullCalendar.php \Drupal\fullcalendar\Plugin\fullcalendar\type\FullCalendar::preView()
  3. 8.2 src/Plugin/fullcalendar/type/FullCalendar.php \Drupal\fullcalendar\Plugin\fullcalendar\type\FullCalendar::preView()
  4. 8.4 src/Plugin/fullcalendar/type/FullCalendar.php \Drupal\fullcalendar\Plugin\fullcalendar\type\FullCalendar::preView()

Overrides FullcalendarBase::preView

File

src/Plugin/fullcalendar/type/FullCalendar.php, line 740

Class

FullCalendar
@todo.

Namespace

Drupal\fullcalendar\Plugin\fullcalendar\type

Code

public function preView(&$settings) {
  if (empty($settings['editable'])) {
    $this->style->view->fullcalendar_disallow_editable = TRUE;
  }

  // TODO provide the ability to alter texts.
  $options = [
    'buttonText' => [
      'day' => $this
        ->t('Day'),
      'week' => $this
        ->t('Week'),
      'month' => $this
        ->t('Month'),
      'today' => $this
        ->t('Today'),
      'listDay' => $this
        ->t('List (day)'),
      'listWeek' => $this
        ->t('List (week)'),
      'listMonth' => $this
        ->t('List (month)'),
      'listYear' => $this
        ->t('List (year)'),
    ],
    'allDayText' => $this
      ->t('All day'),
    'monthNames' => array_values(DateHelper::monthNames(TRUE)),
    'monthNamesShort' => array_values(DateHelper::monthNamesAbbr(TRUE)),
    'dayNames' => DateHelper::weekDays(TRUE),
    'dayNamesShort' => DateHelper::weekDaysAbbr(TRUE),
    'isRTL' => $this->languageManager
      ->getCurrentLanguage()
      ->getDirection() == 'rtl',
  ];
  $advanced = !empty($settings['advanced']);
  foreach ($settings as $key => $value) {
    if (is_array($value)) {
      continue;
    }
    elseif (in_array($key, [
      'left',
      'center',
      'right',
    ])) {
      $options['header'][$key] = $value;
    }
    elseif (in_array($key, [
      'timeformatMonth',
      'timeformatWeek',
      'timeformatDay',
    ])) {
      if ($advanced) {

        // @see https://fullcalendar.io/docs/views/View-Specific-Options/
        $options['views'][strtolower(substr($key, 10))]['timeFormat'] = $value;
      }
    }
    elseif (in_array($key, [
      'columnformatMonth',
      'columnformatWeek',
      'columnformatDay',
    ])) {
      if ($advanced) {

        // @see https://fullcalendar.io/docs/views/View-Specific-Options/
        $options['views'][strtolower(substr($key, 12))]['columnFormat'] = $value;
      }
    }
    elseif (in_array($key, [
      'titleformatMonth',
      'titleformatWeek',
      'titleformatDay',
    ])) {
      if ($advanced) {

        // @see https://fullcalendar.io/docs/views/View-Specific-Options/
        $options['views'][strtolower(substr($key, 11))]['titleFormat'] = $value;
      }
    }
    elseif ($advanced && $key == 'axisFormat') {
      $options[$key] = $value;
    }
    elseif ($key == 'timeformat') {
      if (!$advanced) {
        $options['timeFormat'] = $value;
      }
    }
    elseif ($key == 'contentHeight' && empty($value)) {

      // Don't add this if it is 0.
    }
    elseif ($key == 'advanced') {

      // Don't add this value ever.
    }
    elseif ($key == 'sameWindow' || $key == 'modalWindow') {

      // Keep this at the top level.
      continue;
    }
    else {
      $options[$key] = $value;
    }

    // Unset all values that have been migrated.
    unset($settings[$key]);
  }

  // Add display values.
  if (!empty($settings['display'])) {
    foreach ($settings['display'] as $key => $value) {
      $options[$key] = $value;
    }
    unset($settings['display']);
  }
  $settings['fullcalendar'] = $options;

  // First, use the default date if set.
  if (!empty($settings['times']['default_date'])) {
    list($date['year'], $date['month'], $date['date']) = explode('-', $settings['times']['date']);
    $settings['date'] = $date;
  }

  // Unset times settings.
  unset($settings['times']);

  // Get the values from the URL.
  extract($this->style->view
    ->getExposedInput(), EXTR_SKIP);
  if (isset($year) && is_numeric($year)) {
    $settings['date']['year'] = $year;
  }
  if (isset($month) && is_numeric($month) && $month > 0 && $month <= 12) {
    $settings['date']['month'] = $month;
  }
  if (isset($day) && is_numeric($day) && $day > 0 && $day <= 31) {
    $settings['date']['date'] = $day;
  }
  if (isset($mode) && in_array($mode, [
    'month',
    'basicWeek',
    'basicDay',
    'agendaWeek',
    'agendaDay',
  ])) {
    $settings['date']['defaultView'] = $mode;
  }

  // Ensure that some value is set.
  if (!isset($settings['date']['year'])) {
    $settings['date']['year'] = date('Y', strtotime('now'));
  }
  if (!isset($settings['date']['month'])) {
    $settings['date']['month'] = date('n', strtotime('now'));
  }

  // Change month to zero-based.
  $settings['date']['month']--;
}