Loading src/Plugin/Field/FieldFormatter/OfficeHoursFormatterBase.php +24 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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], ]; } } } src/Plugin/Field/FieldFormatter/OfficeHoursFormatterDefault.php +5 −4 Original line number Diff line number Diff line Loading @@ -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; } Loading src/Plugin/Field/FieldFormatter/OfficeHoursFormatterStatus.php +4 −4 Original line number Diff line number Diff line Loading @@ -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; } Loading src/Plugin/Field/FieldFormatter/OfficeHoursFormatterTable.php +5 −5 Original line number Diff line number Diff line Loading @@ -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; } Loading src/Plugin/Field/FieldType/OfficeHoursItemList.php +10 −4 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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 Loading
src/Plugin/Field/FieldFormatter/OfficeHoursFormatterBase.php +24 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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], ]; } } }
src/Plugin/Field/FieldFormatter/OfficeHoursFormatterDefault.php +5 −4 Original line number Diff line number Diff line Loading @@ -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; } Loading
src/Plugin/Field/FieldFormatter/OfficeHoursFormatterStatus.php +4 −4 Original line number Diff line number Diff line Loading @@ -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; } Loading
src/Plugin/Field/FieldFormatter/OfficeHoursFormatterTable.php +5 −5 Original line number Diff line number Diff line Loading @@ -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; } Loading
src/Plugin/Field/FieldType/OfficeHoursItemList.php +10 −4 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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