Skip to content
Snippets Groups Projects
Unverified Commit e65e7886 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3351706 by plopesc, gxleano, elber, nikhil_110, penyaskito, smustgrave,...

Issue #3351706 by plopesc, gxleano, elber, nikhil_110, penyaskito, smustgrave, quietone, longwave, gábor hojtsy, pameeela: Provide a block for clearing caches from a dashboard
parent e1e072ff
No related branches found
No related tags found
No related merge requests found
......@@ -19,8 +19,17 @@ class PerformanceController extends ControllerBase {
* configuration form.
*/
public function build(): array {
// Load the cache form and embed it in a details element.
$cache_clear = $this->formBuilder()->getForm(ClearCacheForm::class);
$cache_clear['clear_cache'] = [
'#type' => 'details',
'#title' => $this->t('Clear cache'),
'#open' => TRUE,
'clear' => $cache_clear['clear'],
];
unset($cache_clear['clear']);
return [
'cache_clear' => $this->formBuilder()->getForm(ClearCacheForm::class),
'cache_clear' => $cache_clear,
'performance' => $this->formBuilder()->getForm(PerformanceForm::class),
];
}
......
......@@ -23,14 +23,7 @@ public function getFormId() {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['clear_cache'] = [
'#type' => 'details',
'#title' => $this->t('Clear cache'),
'#open' => TRUE,
];
$form['clear_cache']['clear'] = [
$form['clear'] = [
'#type' => 'submit',
'#value' => $this->t('Clear all caches'),
];
......
<?php
namespace Drupal\system\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\system\Form\ClearCacheForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a block to display 'Clear cache' elements.
*/
#[Block(
id: "system_clear_cache_block",
admin_label: new TranslatableMarkup("Clear cache")
)]
class ClearCacheBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Creates a ClearCacheBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
* The form builder.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, protected FormBuilderInterface $formBuilder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('form_builder'),
);
}
/**
* {@inheritdoc}
*/
public function build(): array {
return $this->formBuilder->getForm(ClearCacheForm::class);
}
/**
* {@inheritdoc}
*/
public function blockAccess(AccountInterface $account): AccessResultInterface {
return AccessResult::allowedIfHasPermission($account, 'administer site configuration');
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\system\Functional\Block;
use Drupal\block\BlockInterface;
use Drupal\Tests\BrowserTestBase;
/**
* Tests clear cache block behavior.
*
* @group Block
*
* @see \Drupal\system\Plugin\Block\ClearCacheBlock
*/
class ClearCacheBlockTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The clear cache block instance.
*
* @var \Drupal\block\BlockInterface
*/
protected BlockInterface $clearCacheBlock;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$admin_user = $this->drupalCreateUser(['administer site configuration']);
$this->drupalLogin($admin_user);
$this->clearCacheBlock = $this->placeBlock('system_clear_cache_block', [
'label' => 'Clear cache block',
]);
}
/**
* Tests block behavior and access based on permissions.
*/
public function testCacheClearBlock(): void {
$this->drupalGet('<front>');
$this->assertSession()->pageTextContains('Clear cache block');
$page = $this->getSession()->getPage();
$page->pressButton('Clear all caches');
$this->assertSession()->statusMessageContains('Caches cleared.');
// Confirm that access is not allowed for non-authorized users.
$this->drupalLogout();
$this->drupalGet('<front>');
$this->assertSession()->pageTextNotContains('Clear cache block');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment