Commit e2d424db authored by catch's avatar catch

Issue #2458763 by Wim Leers, m1r1k: Remove the ability to configure a block's cache max-age

parent 3a0b0178
......@@ -295,13 +295,6 @@ block_settings:
label_display:
type: string
label: 'Display title'
cache:
type: mapping
label: 'Cache settings'
mapping:
max_age:
type: integer
label: 'Maximum age'
status:
type: boolean
label: 'Status'
......
......@@ -9,13 +9,11 @@
use Drupal\block\BlockInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Session\AccountInterface;
use Drupal\Component\Transliteration\TransliterationInterface;
......@@ -89,10 +87,6 @@ protected function baseConfigurationDefaults() {
'label' => '',
'provider' => $this->pluginDefinition['provider'],
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
'cache' => array(
// Blocks are cacheable by default.
'max_age' => Cache::PERMANENT,
),
);
}
......@@ -180,23 +174,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#default_value' => ($this->configuration['label_display'] === BlockInterface::BLOCK_LABEL_VISIBLE),
'#return_value' => BlockInterface::BLOCK_LABEL_VISIBLE,
);
// 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(array(\Drupal::service('date.formatter'), 'formatInterval'), array_combine($period, $period));
$period[0] = '<' . $this->t('no caching') . '>';
$period[\Drupal\Core\Cache\Cache::PERMANENT] = $this->t('Forever');
$form['cache'] = array(
'#type' => 'details',
'#title' => $this->t('Cache settings'),
);
$form['cache']['max_age'] = array(
'#type' => 'select',
'#title' => $this->t('Maximum age'),
'#description' => $this->t('The maximum time this block may be cached.'),
'#default_value' => $this->configuration['cache']['max_age'],
'#options' => $period,
);
// Add plugin-specific settings for this block type.
$form += $this->blockForm($form, $form_state);
......@@ -244,7 +221,6 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
$this->configuration['label'] = $form_state->getValue('label');
$this->configuration['label_display'] = $form_state->getValue('label_display');
$this->configuration['provider'] = $form_state->getValue('provider');
$this->configuration['cache'] = $form_state->getValue('cache');
$this->blockSubmit($form, $form_state);
}
}
......@@ -272,16 +248,6 @@ public function getMachineNameSuggestion() {
return $transliterated;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
$max_age = parent::getCacheMaxAge();
// @todo Configurability of this will be removed in
// https://www.drupal.org/node/2458763.
return Cache::mergeMaxAges($max_age, (int) $this->configuration['cache']['max_age']);
}
/**
* Wraps the transliteration service.
*
......
......@@ -102,6 +102,27 @@ function block_update_8002() {
\Drupal::state()->set('block_update_8002_placeholder', TRUE);
}
/**
* Remove 'cache' setting.
*/
function block_update_8003() {
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('block.block.') as $block_config_name) {
$block = $config_factory->getEditable($block_config_name);
// Remove the 'cache' setting.
$settings = $block->get('settings');
unset($settings['cache']);
$block->set('settings', $settings);
// Mark the resulting configuration as trusted data. This avoids issues with
// future schema changes.
$block->save(TRUE);
}
return t('Block settings updated.');
}
/**
* @} End of "addtogroup updates-8.0.0-beta".
*/
......@@ -148,9 +148,7 @@ function testCachePermissions() {
* Test non-cacheable block.
*/
function testNoCache() {
$this->setBlockCacheConfig(array(
'max_age' => 0,
));
\Drupal::state()->set('block_test.cache_max_age', 0);
$current_content = $this->randomMachineName();
\Drupal::state()->set('block_test.content', $current_content);
......@@ -219,13 +217,4 @@ function testCachePerPage() {
$this->assertText($old_content, 'Block content cached for the test page.');
}
/**
* Private helper method to set the test block's cache configuration.
*/
private function setBlockCacheConfig($cache_config) {
$block = $this->block->getPlugin();
$block->setConfigurationValue('cache', $cache_config);
$this->block->save();
}
}
......@@ -7,7 +7,6 @@
namespace Drupal\block\Tests;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormState;
use Drupal\simpletest\KernelTestBase;
use Drupal\block\BlockInterface;
......@@ -44,9 +43,6 @@ public function testBlockInterface() {
'label' => 'Custom Display Message',
'provider' => 'block_test',
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
'cache' => array(
'max_age' => Cache::PERMANENT,
),
'display_message' => 'no message set',
);
// Initial configuration of the block at construction time.
......@@ -60,10 +56,6 @@ public function testBlockInterface() {
$this->assertIdentical($display_block->getConfiguration(), $expected_configuration, 'The block configuration was updated correctly.');
$definition = $display_block->getPluginDefinition();
$period = array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400);
$period = array_map(array(\Drupal::service('date.formatter'), 'formatInterval'), array_combine($period, $period));
$period[0] = '<' . t('no caching') . '>';
$period[\Drupal\Core\Cache\Cache::PERMANENT] = t('Forever');
$expected_form = array(
'provider' => array(
'#type' => 'value',
......@@ -87,17 +79,6 @@ public function testBlockInterface() {
'#default_value' => TRUE,
'#return_value' => 'visible',
),
'cache' => array(
'#type' => 'details',
'#title' => t('Cache settings'),
'max_age' => array(
'#type' => 'select',
'#title' => t('Maximum age'),
'#description' => t('The maximum time this block may be cached.'),
'#default_value' => Cache::PERMANENT,
'#options' => $period,
),
),
'display_message' => array(
'#type' => 'textfield',
'#title' => t('Display message'),
......
......@@ -7,7 +7,6 @@
namespace Drupal\block\Tests;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\simpletest\KernelTestBase;
use Drupal\block_test\Plugin\Block\TestHtmlBlock;
......@@ -99,9 +98,6 @@ protected function createTests() {
'label' => '',
'provider' => 'block_test',
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
'cache' => array(
'max_age' => Cache::PERMANENT,
),
),
'visibility' => array(),
);
......
......@@ -8,6 +8,7 @@
namespace Drupal\block_test\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
/**
* Provides a block to test caching.
......@@ -39,4 +40,11 @@ public function getCacheContexts() {
return \Drupal::state()->get('block_test.cache_contexts', []);
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return \Drupal::state()->get('block_test.cache_max_age', parent::getCacheMaxAge());
}
}
......@@ -50,27 +50,4 @@ public function build() {
);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
// @see ::getCacheMaxAge()
$form['cache']['#disabled'] = TRUE;
$form['cache']['max_age']['#value'] = Cache::PERMANENT;
$form['cache']['#description'] = $this->t('This block is always cached forever, it is not configurable.');
return $form;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
// The 'Syndicate' block is permanently cacheable, because its
// contents can never change.
return Cache::PERMANENT;
}
}
......@@ -410,7 +410,6 @@ protected function drupalBuildEntityView(EntityInterface $entity, $view_mode = '
* - region: 'sidebar_first'.
* - theme: The default theme.
* - visibility: Empty array.
* - cache: array('max_age' => Cache::PERMANENT).
*
* @return \Drupal\block\Entity\Block
* The block entity.
......@@ -427,9 +426,6 @@ protected function drupalPlaceBlock($plugin_id, array $settings = array()) {
'label' => $this->randomMachineName(8),
'visibility' => array(),
'weight' => 0,
'cache' => array(
'max_age' => Cache::PERMANENT,
),
);
$values = [];
foreach (array('region', 'id', 'theme', 'plugin', 'weight', 'visibility') as $key) {
......
......@@ -43,17 +43,4 @@ public function build() {
return $this->mainContent;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['cache']['#disabled'] = TRUE;
$form['cache']['#description'] = $this->t("This block's maximum age cannot be configured, because it depends on the contents.");
$form['cache']['max_age']['#value'] = Cache::PERMANENT;
return $form;
}
}
......@@ -40,20 +40,6 @@ public function build() {
return ['#type' => 'status_messages'];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
// @see ::getCacheMaxAge()
$form['cache']['#description'] = $this->t('This block is cacheable forever, it is not configurable.');
$form['cache']['max_age']['#value'] = Cache::PERMANENT;
$form['cache']['max_age']['#disabled'] = TRUE;
return $form;
}
/**
* {@inheritdoc}
*/
......
......@@ -28,27 +28,4 @@ public function build() {
return array('#markup' => '<span>' . $this->t('Powered by <a href="@poweredby">Drupal</a>', array('@poweredby' => 'https://www.drupal.org')) . '</span>');
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
// @see ::getCacheMaxAge()
$form['cache']['#disabled'] = TRUE;
$form['cache']['max_age']['#value'] = Cache::PERMANENT;
$form['cache']['#description'] = $this->t('This block is always cached forever, it is not configurable.');
return $form;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
// The 'Powered by Drupal' block is permanently cacheable, because its
// contents can never change.
return Cache::PERMANENT;
}
}
......@@ -49,7 +49,7 @@ public function testBlockForms() {
$this->drupalLogin($this->rootUser);
$this->drupalPlaceBlock('search_form_block', ['weight' => -5]);
$this->drupalPlaceBlock('ajax_forms_test_block', ['cache' => ['max_age' => 0]]);
$this->drupalPlaceBlock('ajax_forms_test_block');
$this->drupalGet('');
$this->drupalPostAjaxForm(NULL, ['test1' => 'option1'], 'test1');
......@@ -66,7 +66,7 @@ public function testQueryString() {
$this->container->get('module_installer')->install(['block']);
$this->drupalLogin($this->rootUser);
$this->drupalPlaceBlock('ajax_forms_test_block', ['cache' => ['max_age' => 0]]);
$this->drupalPlaceBlock('ajax_forms_test_block');
$url = Url::fromRoute('entity.user.canonical', ['user' => $this->rootUser->id()], ['query' => ['foo' => 'bar']]);
$this->drupalGet($url);
......
......@@ -1513,7 +1513,6 @@ function system_update_8006() {
'region' => 'content',
'settings.label' => 'Site branding',
'settings.label_display' => 0,
'settings.cache.max_age' => 0,
'visibility' => [],
'weight' => 0,
'langcode' => $langcode,
......
......@@ -7,7 +7,6 @@
namespace Drupal\views\Tests\Plugin;
use Drupal\Core\Cache\Cache;
use Drupal\views\Tests\ViewKernelTestBase;
/**
......@@ -83,7 +82,6 @@ public function testViewsBlock() {
* - region: 'sidebar_first'.
* - theme: The default theme.
* - visibility: Empty array.
* - cache: array('max_age' => Cache::PERMANENT).
*
* @return \Drupal\block\Entity\Block
* The block entity.
......@@ -97,9 +95,6 @@ protected function createBlock($plugin_id, array $settings = array()) {
'label' => $this->randomMachineName(8),
'visibility' => array(),
'weight' => 0,
'cache' => array(
'max_age' => Cache::PERMANENT,
),
);
$values = [];
foreach (array('region', 'id', 'theme', 'plugin', 'weight', 'visibility') as $key) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment