core/includes/bootstrap.inc | 4 ++++ .../aggregator/Plugin/Block/AggregatorFeedBlock.php | 3 ++- core/modules/block/config/schema/block.schema.yml | 8 ++++++-- core/modules/block/lib/Drupal/block/BlockBase.php | 15 +++++++++------ .../lib/Drupal/block/Tests/BlockInterfaceTest.php | 13 +++++++------ .../Drupal/book/Plugin/Block/BookNavigationBlock.php | 3 ++- .../lib/Drupal/node/Plugin/Block/SyndicateBlock.php | 3 ++- .../Plugin/Block/StatisticsPopularBlock.php | 3 ++- .../system/lib/Drupal/system/Form/PerformanceForm.php | 6 ++++-- .../Drupal/system/Plugin/Block/SystemMenuBlock.php | 17 +++++++++++++++++ 10 files changed, 55 insertions(+), 20 deletions(-) diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 8e24668..caaa20c 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -974,6 +974,10 @@ function drupal_serve_page_from_cache(stdClass $cache, Response $response, Reque // max-age > 0, allowing the page to be cached by external proxies, when a // session cookie is present unless the Vary header has been replaced. $max_age = !$request->cookies->has(session_name()) || isset($boot_headers['vary']) ? $config->get('cache.page.max_age') : 0; + // Never set an expiration date further than one year into the future. + if ($max_age > 31536000 || $max_age === \Drupal\Core\Cache\Cache::PERMANENT) { + $max_age = 31536000; + } $response->headers->set('Cache-Control', 'public, max-age=' . $max_age); // Entity tag should change if the output changes. diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorFeedBlock.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorFeedBlock.php index 4547527..66cb565 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorFeedBlock.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/Block/AggregatorFeedBlock.php @@ -79,7 +79,8 @@ public static function create(ContainerInterface $container, array $configuratio */ public function defaultConfiguration() { // By default, the block will contain 10 feed items. - return array( + $default_configuration = parent::defaultConfiguration(); + return $default_configuration + array( 'block_count' => 10, 'feed' => NULL, ); diff --git a/core/modules/block/config/schema/block.schema.yml b/core/modules/block/config/schema/block.schema.yml index 1ed3f49..24660b1 100644 --- a/core/modules/block/config/schema/block.schema.yml +++ b/core/modules/block/config/schema/block.schema.yml @@ -73,8 +73,12 @@ block.block.*: type: string label: 'Display title' cache: - type: integer - label: 'Cache' + type: mapping + label: 'Cache settings' + mapping: + max_age: + type: integer + label: 'Maximum age' status: type: boolean label: 'Status' diff --git a/core/modules/block/lib/Drupal/block/BlockBase.php b/core/modules/block/lib/Drupal/block/BlockBase.php index 8185a18..6e99c48 100644 --- a/core/modules/block/lib/Drupal/block/BlockBase.php +++ b/core/modules/block/lib/Drupal/block/BlockBase.php @@ -113,15 +113,18 @@ public function buildConfigurationForm(array $form, array &$form_state) { '#default_value' => ($this->configuration['label_display'] === BlockInterface::BLOCK_LABEL_VISIBLE), '#return_value' => BlockInterface::BLOCK_LABEL_VISIBLE, ); - $ages = array(0, 60, 300, 1800, 3600, 21600, 86400, 604800, 2592000, 31536000); - $options = array_map(array(\Drupal::service('date'), 'formatInterval'), array_combine($ages, $ages)); - $options[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever'); + // Identical options to the ones for page caching. + // @see \Drupal\system\Form\PerformanceForm::buildForm() + $period = array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400); + $period = array_map('format_interval', array_combine($period, $period)); + $period[0] = '<' . t('no caching') . '>'; + $period[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever'); $form['cache']['max_age'] = array( '#type' => 'select', - '#title' => t('Cache: Max age'), - '#description' => t('Choose how long this block may be cached. The default of 0 seconds means it will never be cached.'), + '#title' => t('Cache: Maximum age'), + '#description' => t('The maximum time a block can be cached.'), '#default_value' => $this->configuration['cache']['max_age'], - '#options' => $options, + '#options' => $period, ); // Add plugin-specific settings for this block type. $form += $this->blockForm($form, $form_state); diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockInterfaceTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockInterfaceTest.php index f9c0921..ab4f4c1 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockInterfaceTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockInterfaceTest.php @@ -62,9 +62,10 @@ public function testBlockInterface() { $this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block configuration was updated correctly.'); $definition = $display_block->getPluginDefinition(); - $ages = array(0, 60, 300, 1800, 3600, 21600, 86400, 604800, 2592000, 31536000); - $options = array_map(array(\Drupal::service('date'), 'formatInterval'), array_combine($ages, $ages)); - $options[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever'); + $period = array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400); + $period = array_map('format_interval', array_combine($period, $period)); + $period[0] = '<' . t('no caching') . '>'; + $period[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever'); $expected_form = array( 'module' => array( '#type' => 'value', @@ -91,10 +92,10 @@ public function testBlockInterface() { 'cache' => array( 'max_age' => array( '#type' => 'select', - '#title' => t('Cache: Max age'), - '#description' => t('Choose how long this block may be cached. The default of 0 seconds means it will never be cached.'), + '#title' => t('Cache: Maximum age'), + '#description' => t('The maximum time a block can be cached.'), '#default_value' => 0, - '#options' => $options, + '#options' => $period, ), ), 'display_message' => array( diff --git a/core/modules/book/lib/Drupal/book/Plugin/Block/BookNavigationBlock.php b/core/modules/book/lib/Drupal/book/Plugin/Block/BookNavigationBlock.php index e2cf674..e82fa11 100644 --- a/core/modules/book/lib/Drupal/book/Plugin/Block/BookNavigationBlock.php +++ b/core/modules/book/lib/Drupal/book/Plugin/Block/BookNavigationBlock.php @@ -64,7 +64,8 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function defaultConfiguration() { - return array( + $default_configuration = parent::defaultConfiguration(); + return $default_configuration + array( 'block_mode' => "all pages", ); } diff --git a/core/modules/node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php b/core/modules/node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php index ebcacf6..588cb48 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php +++ b/core/modules/node/lib/Drupal/node/Plugin/Block/SyndicateBlock.php @@ -25,7 +25,8 @@ class SyndicateBlock extends BlockBase { * {@inheritdoc} */ public function defaultConfiguration() { - return array( + $default_configuration = parent::defaultConfiguration(); + return $default_configuration + array( 'block_count' => 10, ); } diff --git a/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php b/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php index 1a19761..85b05a3 100644 --- a/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php +++ b/core/modules/statistics/lib/Drupal/statistics/Plugin/Block/StatisticsPopularBlock.php @@ -45,7 +45,8 @@ class StatisticsPopularBlock extends BlockBase { * {@inheritdoc} */ public function defaultConfiguration() { - return array( + $default_configuration = parent::defaultConfiguration(); + return $default_configuration + array( 'top_day_num' => 0, 'top_all_num' => 0, 'top_last_num' => 0 diff --git a/core/modules/system/lib/Drupal/system/Form/PerformanceForm.php b/core/modules/system/lib/Drupal/system/Form/PerformanceForm.php index 6e7d9d6..db3920e 100644 --- a/core/modules/system/lib/Drupal/system/Form/PerformanceForm.php +++ b/core/modules/system/lib/Drupal/system/Form/PerformanceForm.php @@ -79,10 +79,12 @@ public function buildForm(array $form, array &$form_state) { '#title' => t('Caching'), '#open' => TRUE, ); - + // Identical options to the ones for block caching. + // @see \Drupal\block\BlockBase::buildConfigurationForm() $period = array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400); $period = array_map('format_interval', array_combine($period, $period)); - $period[0] = '<' . t('none') . '>'; + $period[0] = '<' . t('no caching') . '>'; + $period[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever'); $form['caching']['page_cache_maximum_age'] = array( '#type' => 'select', '#title' => t('Page cache maximum age'), diff --git a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php index f44e533..8a5e372 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php @@ -32,6 +32,23 @@ public function build() { /** * {@inheritdoc} */ + public function defaultConfiguration() { + $default_configuration = parent::defaultConfiguration(); + // Modify the default max age for menu blocks: modifications made to menus, + // menu links and menu blocks will automatically invalidate corresponding + // cache tags, therefor allowing us to cache menu blocks forever. This is + // only not the case if there are user-specific or dynamic alterations (e.g. + // hook_node_access()), but in that: + // 1) it is possible to set a different max age for individual blocks, since + // this is just the default value. + // 2) modules can modify caching by implementing hook_block_view_alter() + $default_configuration['cache']['max_age'] = \Drupal\Core\Cache\Cache::PERMANENT; + return $default_configuration; + } + + /** + * {@inheritdoc} + */ public function getCacheKeys() { return array(DRUPAL_CACHE_PER_PAGE, DRUPAL_CACHE_PER_ROLE); }