TestBlockInstantiation.php in Drupal 10
File
core/modules/block/tests/modules/block_test/src/Plugin/Block/TestBlockInstantiation.php
View source
<?php
namespace Drupal\block_test\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
class TestBlockInstantiation extends BlockBase {
public function defaultConfiguration() {
return [
'display_message' => 'no message set',
];
}
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIfHasPermission($account, 'access content');
}
public function blockForm($form, FormStateInterface $form_state) {
$form['display_message'] = [
'#type' => 'textfield',
'#title' => $this
->t('Display message'),
'#default_value' => $this->configuration['display_message'],
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['display_message'] = $form_state
->getValue('display_message');
}
public function build() {
return [
'#children' => $this->configuration['display_message'],
];
}
}