You are here

public function SmartDateDailyRangeFormatter::viewElements in Smart Date 3.0.x

Same name and namespace in other branches
  1. 3.1.x modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateDailyRangeFormatter.php \Drupal\smart_date_recur\Plugin\Field\FieldFormatter\SmartDateDailyRangeFormatter::viewElements()
  2. 3.2.x modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateDailyRangeFormatter.php \Drupal\smart_date_recur\Plugin\Field\FieldFormatter\SmartDateDailyRangeFormatter::viewElements()
  3. 3.3.x modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateDailyRangeFormatter.php \Drupal\smart_date_recur\Plugin\Field\FieldFormatter\SmartDateDailyRangeFormatter::viewElements()
  4. 3.4.x modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateDailyRangeFormatter.php \Drupal\smart_date_recur\Plugin\Field\FieldFormatter\SmartDateDailyRangeFormatter::viewElements()

Overrides SmartDateTrait::viewElements

File

modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateDailyRangeFormatter.php, line 54

Class

SmartDateDailyRangeFormatter
Plugin for a recurrence-optimized formatter for 'smartdate' fields.

Namespace

Drupal\smart_date_recur\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];

  // TODO: intellident switching between retrieval methods
  // Look for a defined format and use it if specified.
  $format_label = $this
    ->getSetting('format');
  if ($format_label) {
    $format = SmartDateFormat::load($format_label);
    $settings = $format
      ->getOptions();
  }
  else {
    $settings = [
      'separator' => $this
        ->getSetting('separator'),
      'join' => $this
        ->getSetting('join'),
      'time_format' => $this
        ->getSetting('time_format'),
      'time_hour_format' => $this
        ->getSetting('time_hour_format'),
      'date_format' => $this
        ->getSetting('date_format'),
      'date_first' => $this
        ->getSetting('date_first'),
      'ampm_reduce' => $this
        ->getSetting('ampm_reduce'),
      'allday_label' => $this
        ->getSetting('allday_label'),
    ];
  }
  $add_classes = $this
    ->getSetting('add_classes');
  $no_date_format = $settings;
  $no_date_format['date_format'] = '';
  $no_time_format = $settings;
  $no_time_format['time_format'] = '';
  $rrules = [];
  $rrules_nondaily = [];
  foreach ($items as $delta => $item) {
    $timezone = $item->timezone ? $item->timezone : NULL;
    if (empty($item->value) || empty($item->end_value)) {
      continue;
    }
    $is_daily = FALSE;
    if (!empty($item->rrule)) {
      if (isset($rrules[$item->rrule])) {

        // Already established as daily.
        $is_daily = TRUE;
      }
      elseif (isset($rrules_nondaily[$item->rrule])) {

        // Already established as NOT daily, so list as normal.
      }
      else {
        $rrule_obj = SmartDateRule::load($item->rrule);
        $rule_props = $rrule_obj
          ->toArray();

        // Check that no extra parameters have been set.
        // TODO: Separate handling for daily ranges with no end?
        // TODO: Check for overrides.
        if ($rule_props['freq'] && $rule_props['freq'][0]['value'] == 'DAILY' && $rule_props['limit'] && !$rule_props['parameters']) {

          // Uses a daily rule, so render a range instead.
          $is_daily = TRUE;
          $elements[$delta] = $item->rrule;
          $rrules[$item->rrule]['delta'] = $delta;
        }
        else {
          $rrules_nondaily[$item->rrule]['delta'] = $delta;
        }
      }
    }
    if ($is_daily) {

      // Add this instance to our array of instances for the rule.
      $rrules[$item->rrule]['instances'][] = $item;
    }
    else {

      // No rule so include the item directly.
      $elements[$delta] = static::formatSmartDate($item->value, $item->end_value, $settings, $timezone);
      if ($add_classes) {
        $this
          ->addRangeClasses($elements[$delta]);
      }
    }
  }
  foreach ($rrules as $rrule_collected) {
    $instances = $rrule_collected['instances'];
    if (empty($instances)) {
      continue;
    }
    $first_date = array_shift($instances);
    $last_date = array_pop($instances);
    $output['time'] = static::formatSmartDate($first_date->value, $first_date->end_value, $no_date_format, $timezone);
    $output['join'] = [
      '#markup' => $settings['join'],
    ];
    $output['date'] = static::formatSmartDate($first_date->value, $last_date->end_value, $no_time_format, $timezone);
    if ($settings['date_first']) {

      // Time should be first so reverse the array.
      ksort($output);
    }
    $output['#attributes']['class'] = [
      'smart_date_range',
    ];
    $temp_array['start'] = $output;
    if ($add_classes) {
      $this
        ->addRangeClasses($temp_array);
    }
    $delta = $rrule_collected['delta'];
    $elements[$delta] = $temp_array['start'];
  }
  return $elements;
}