Commit 87a94e31 authored by John Voskuilen's avatar John Voskuilen
Browse files

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

parent 6ce352da
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\office_hours;

use Drupal\Component\Utility\Html;
use Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItem;

/**
 * Factors out OfficeHoursItemList->getItems()->getRows().
@@ -22,6 +23,8 @@ trait OfficeHoursFormatterTrait {
   *   The settings.
   * @param array $field_settings
   *   The field settings.
   * @param array $third_party_settings
   *   The third party settings.
   * @param $time
   *   A time stamp. Defaults to 'REQUEST_TIME'.
   *
@@ -449,4 +452,39 @@ trait OfficeHoursFormatterTrait {
    return $office_hours;
  }

  /**
   * 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) {
      if (!$item->isExceptionDay()) {
        return TRUE;
      }
      if (self::$horizon == NULL) {
        // Exceptions settings are not set / submodule is disabled.
        return FALSE;
      }
      if ($item->isExceptionDayInRange(self::$horizon)) {
        return TRUE;
      }
      return FALSE;
    });
    return $this;
  }

}