Commit 6ce352da authored by John Voskuilen's avatar John Voskuilen
Browse files

Issue #1998266 by johnv: Add Exception day feature - prepare standard code

parent 510e2273
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ class OfficeHoursFormatterDefault extends OfficeHoursFormatterBase {
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemList $items */
    $elements = [];

    // If no data is filled for this entity, do not show the formatter.
@@ -43,6 +44,12 @@ class OfficeHoursFormatterDefault extends OfficeHoursFormatterBase {
    // N.B. 'Show current day' may return nothing in getRows(), while other days are filled.
    /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */
    $office_hours = $items->getRows($settings, $this->getFieldSettings(), $third_party_settings);

    // If no data is filled for this entity, do not show the formatter.
    if ($items->isEmpty()) {
      return $elements;
    }

    $elements[] = [
      '#theme' => 'office_hours',
      '#parent' => $field_definition,
+7 −0
Original line number Diff line number Diff line
@@ -30,8 +30,10 @@ class OfficeHoursFormatterTable extends OfficeHoursFormatterBase {
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemList $items */
    $elements = [];

    // If no data is filled for this entity, do not show the formatter.
    if ($items->isEmpty()) {
      return $elements;
    }
@@ -43,6 +45,11 @@ class OfficeHoursFormatterTable extends OfficeHoursFormatterBase {
    /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */
    $office_hours = $items->getRows($settings, $this->getFieldSettings(), $third_party_settings);

    // If no data is filled for this entity, do not show the formatter.
    if ($items->isEmpty()) {
      return $elements;
    }

    // For a11y screen readers, a header is introduced.
    // Superfluous comments are removed. @see #3110755 for examples and explanation.
    $isLabelEnabled = $settings['day_format'] != 'none';
+2 −32
Original line number Diff line number Diff line
@@ -65,6 +65,8 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn
   * {@inheritdoc}
   */
  public function getRows(array $settings, array $field_settings, array $third_party_settings, $time = NULL) {
    // @todo move more from getRows here, using itemList, not values.
    $this->keepExceptionDaysInHorizon($third_party_settings['office_hours_exceptions']['restrict_exceptions_to_num_days'] ?? NULL);
    return $this->getFieldRows($this->getValue(), $settings, $field_settings, $third_party_settings, $time);
  }

@@ -185,38 +187,6 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn
    parent::setValue($value, $notify);
  }

  /**
   * A helper variable for keepExceptionDaysInHorizon.
   *
   * @var int
   */
  protected static $horizon;

  /**
   * Formatter: remove all Exception days behind horizon.
   *
   * @param int $horizon
   *   The number of days in the future.
   *
   * @return \Drupal\Core\Field\FieldItemListInterface
   *   Filtered Item list.
   */
  public function keepExceptionDaysInHorizon($horizon) {
    self::$horizon = $horizon;

    $this->filter(function (OfficeHoursItem $item) {
      /** @var \Drupal\office_hours_exceptions\Plugin\Field\FieldType\OfficeHoursExceptionsItem $item */
      if (!$item->isExceptionDay()) {
        return TRUE;
      }
      if ($item->isExceptionDayInRange(self::$horizon)) {
        return TRUE;
      }
      return FALSE;
    });
    return $this;
  }

  /**
   * Dispatches an event.
   *