Commit 63498a59 authored by John Voskuilen's avatar John Voskuilen
Browse files

Issue #2767779: Fix Field Caching for formatter

parent 57bcdb00
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\office_hours\Plugin\Field\FieldFormatter;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
@@ -400,4 +401,27 @@ abstract class OfficeHoursFormatterBase extends FormatterBase {
    return $elements;
  }

  /**
   * Add a ['#cache']['max-age'] attribute to $elements, if necessary.
   *
   * @param OfficeHoursItemListInterface $items
   * @param array $elements
   */
  protected function addCacheMaxAge(OfficeHoursItemListInterface $items, array &$elements) {
    $field_definition = $items->getFieldDefinition();
    $settings = $this->getSettings();

    $max_age = $items->getCacheTime($settings, $this->getFieldSettings());
    if ($max_age !== Cache::PERMANENT) {
      // $cache_tags = $field_definition->getTargetEntityTypeId();
      $cache_tags = $field_definition->id();
      $elements['#cache'] = [
        'max-age' => $items->getStatusTimeLeft($settings, $this->getFieldSettings()),
        // @todo Add per-entity tags 'thing:identifier'.
        // @see https://www.drupal.org/docs/drupal-apis/cache-api/cache-tags
        'tags' => [$cache_tags],
      ];
    }
  }

}
+5 −4
Original line number Diff line number Diff line
@@ -68,14 +68,15 @@ class OfficeHoursFormatterDefault extends OfficeHoursFormatterBase {
          'office_hours/office_hours_formatter',
        ],
      ],
      '#cache' => [
        'max-age' => $items->getStatusTimeLeft($settings, $this->getFieldSettings()),
        'tags' => ['office_hours:field.default'],
      ],
    ];

    $elements = $this->addSchemaFormatter($items, $langcode, $elements);
    $elements = $this->addStatusFormatter($items, $langcode, $elements);

    // Add a ['#cache']['max-age'] attribute to $elements.
    // Note: This invalidates a previous Cache in Status Formatter.
    $this->addCacheMaxAge($items, $elements);

    return $elements;
  }

+4 −4
Original line number Diff line number Diff line
@@ -60,12 +60,12 @@ class OfficeHoursFormatterStatus extends OfficeHoursFormatterBase {
      '#open_text' => (string) $this->t($settings['current_status']['open_text']),
      '#closed_text' => (string) $this->t($settings['current_status']['closed_text']),
      '#position' => $this->settings['current_status']['position'],
      '#cache' => [
        'max-age' => $items->getStatusTimeLeft($settings, $this->getFieldSettings()),
        'tags' => ['office_hours:field.status'],
      ],
    ];

    // Add a ['#cache']['max-age'] attribute to $elements.
    // Note: This invalidates a previous Cache in Status Formatter.
    $this->addCacheMaxAge($items, $elements);

    return $elements;
  }

+5 −5
Original line number Diff line number Diff line
@@ -128,15 +128,15 @@ class OfficeHoursFormatterTable extends OfficeHoursFormatterBase {
      '#office_hours' => $office_hours,
      // Pass (unfiltered) office_hours items to twig theming.
      '#office_hours_field' => $items,
      '#cache' => [
        'max-age' => $items->getStatusTimeLeft($settings, $this->getFieldSettings()),
        'tags' => ['office_hours:field.table'],
      ],

    ];

    $elements = $this->addSchemaFormatter($items, $langcode, $elements);
    $elements = $this->addStatusFormatter($items, $langcode, $elements);

    // Add a ['#cache']['max-age'] attribute to $elements.
    // Note: This invalidates a previous Cache in Status Formatter.
    $this->addCacheMaxAge($items, $elements);

    return $elements;
  }

+10 −4
Original line number Diff line number Diff line
@@ -173,9 +173,9 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn
  /**
   * {@inheritdoc}
   */
  public function getStatusTimeLeft(array $settings, array $field_settings) {
  public function getCacheTime(array $settings, array $field_settings) {

    // @see https://www.drupal.org/docs/8/api/cache-api/cache-max-age
    // @see https://www.drupal.org/docs/drupal-apis/cache-api/cache-max-age
    // If there are no open days, cache forever.
    if ($this->isEmpty()) {
      return Cache::PERMANENT;
@@ -188,9 +188,15 @@ class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListIn

    $next_time = '0000';
    $add_days = 0;

    // Take the 'open/closed' indicator, if set, since it is the lowest.
    $cache_setting = $settings['show_closed'];
    if ($settings['current_status']['position'] !== '') {
      $cache_setting = 'next';
    }
    // Get some settings from field. Do not overwrite defaults.
    // Return the filtered days/slots/items/rows.
    switch ($settings['show_closed']) {
    switch ($cache_setting) {
      case 'all':
      case 'open':
      case 'none':
Loading