namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
class ConfigurableBlock extends BlockBase {
public function build(): array {
$config = $this->getConfiguration();
return [
'#markup' => $this->t('Configurable value: @value', ['@value' => $config['block_value'] ?? 'Default']),
];
}
public function blockForm(array $form, FormStateInterface $form_state): array {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
$form['block_value'] = [
'#type' => 'textfield',
'#title' => $this->t('Block Value'),
'#default_value' => $config['block_value'] ?? '',
];
return $form;
}
public function blockSubmit(array &$form, FormStateInterface $form_state): void {
$this->setConfigurationValue('block_value', $form_state->getValue('block_value'));
}
}