You are here

protected function DateRecurBasicFormatter::viewItem in Recurring Dates Field 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php \Drupal\date_recur\Plugin\Field\FieldFormatter\DateRecurBasicFormatter::viewItem()
  2. 3.0.x src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php \Drupal\date_recur\Plugin\Field\FieldFormatter\DateRecurBasicFormatter::viewItem()
  3. 3.1.x src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php \Drupal\date_recur\Plugin\Field\FieldFormatter\DateRecurBasicFormatter::viewItem()

Generate the output appropriate for a field item.

Parameters

\Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem $item: A field item.

int $maxOccurrences: Maximum number of occurrences to show for this field item.

Return value

array A render array for a field item.

1 call to DateRecurBasicFormatter::viewItem()
DateRecurBasicFormatter::viewElements in src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php

File

src/Plugin/Field/FieldFormatter/DateRecurBasicFormatter.php, line 339

Class

DateRecurBasicFormatter
Basic recurring date formatter.

Namespace

Drupal\date_recur\Plugin\Field\FieldFormatter

Code

protected function viewItem(DateRecurItem $item, $maxOccurrences) : array {
  $cacheability = new CacheableMetadata();
  $build = [
    '#theme' => 'date_recur_basic_formatter',
    '#is_recurring' => $item
      ->isRecurring(),
  ];
  $startDate = $item->start_date;

  /** @var \Drupal\Core\Datetime\DrupalDateTime|null $endDate */
  $endDate = $item->end_date ?? $startDate;
  if (!$startDate || !$endDate) {
    return $build;
  }
  $build['#date'] = $this
    ->buildDateRangeValue($startDate, $endDate, FALSE);

  // Render the rule.
  if ($item
    ->isRecurring() && $this
    ->getSetting('interpreter')) {

    /** @var string|null $interpreterId */
    $interpreterId = $this
      ->getSetting('interpreter');
    if ($interpreterId && ($interpreter = $this->dateRecurInterpreterStorage
      ->load($interpreterId))) {
      assert($interpreter instanceof DateRecurInterpreterInterface);
      $rules = $item
        ->getHelper()
        ->getRules();
      $plugin = $interpreter
        ->getPlugin();
      $cacheability
        ->addCacheableDependency($interpreter);
      $build['#interpretation'] = $plugin
        ->interpret($rules, 'en');
    }
  }

  // Occurrences are generated even if the item is not recurring.
  $build['#occurrences'] = array_map(function (DateRange $occurrence) : array {
    $startDate = DrupalDateTime::createFromDateTime($occurrence
      ->getStart());
    $endDate = DrupalDateTime::createFromDateTime($occurrence
      ->getEnd());
    return $this
      ->buildDateRangeValue($startDate, $endDate, TRUE);
  }, $this
    ->getOccurrences($item, $maxOccurrences));
  $cacheability
    ->applyTo($build);
  return $build;
}