class ExampleConfigurableTextBlock
Same name in other branches
- 3.x modules/block_example/src/Plugin/Block/ExampleConfigurableTextBlock.php \Drupal\block_example\Plugin\Block\ExampleConfigurableTextBlock
- 4.0.x modules/block_example/src/Plugin/Block/ExampleConfigurableTextBlock.php \Drupal\block_example\Plugin\Block\ExampleConfigurableTextBlock
Provides a 'Example: configurable text string' block.
Drupal\Core\Block\BlockBase gives us a very useful set of basic functionality for this configurable block. We can just fill in a few of the blanks with defaultConfiguration(), blockForm(), blockSubmit(), and build().
Plugin annotation
@Block(
id = "example_configurable_text",
admin_label = @Translation("Example: configurable text")
)
Hierarchy
- class \Drupal\block_example\Plugin\Block\ExampleConfigurableTextBlock extends \Drupal\Core\Block\BlockBase
Expanded class hierarchy of ExampleConfigurableTextBlock
File
-
block_example/
src/ Plugin/ Block/ ExampleConfigurableTextBlock.php, line 20
Namespace
Drupal\block_example\Plugin\BlockView source
class ExampleConfigurableTextBlock extends BlockBase {
/**
* {@inheritdoc}
*
* This method sets the block default configuration. This configuration
* determines the block's behavior when a block is initially placed in a
* region. Default values for the block configuration form should be added to
* the configuration array. System default configurations are assembled in
* BlockBase::__construct() e.g. cache setting and block title visibility.
*
* @see \Drupal\block\BlockBase::__construct()
*/
public function defaultConfiguration() {
return [
'block_example_string' => $this->t('A default value. This block was created at %time', [
'%time' => date('c'),
]),
];
}
/**
* {@inheritdoc}
*
* This method defines form elements for custom block configuration. Standard
* block configuration fields are added by BlockBase::buildConfigurationForm()
* (block title and title visibility) and BlockFormController::form() (block
* visibility settings).
*
* @see \Drupal\block\BlockBase::buildConfigurationForm()
* @see \Drupal\block\BlockFormController::form()
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['block_example_string_text'] = [
'#type' => 'textarea',
'#title' => $this->t('Block contents'),
'#description' => $this->t('This text will appear in the example block.'),
'#default_value' => $this->configuration['block_example_string'],
];
return $form;
}
/**
* {@inheritdoc}
*
* This method processes the blockForm() form fields when the block
* configuration form is submitted.
*
* The blockValidate() method can be used to validate the form submission.
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['block_example_string'] = $form_state->getValue('block_example_string_text');
}
/**
* {@inheritdoc}
*/
public function build() {
return [
'#markup' => $this->configuration['block_example_string'],
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
ExampleConfigurableTextBlock::blockForm | public | function | This method defines form elements for custom block configuration. Standard block configuration fields are added by BlockBase::buildConfigurationForm() (block title and title visibility) and BlockFormController::form() (block visibility settings). |
ExampleConfigurableTextBlock::blockSubmit | public | function | This method processes the blockForm() form fields when the block configuration form is submitted. |
ExampleConfigurableTextBlock::build | public | function | |
ExampleConfigurableTextBlock::defaultConfiguration | public | function | This method sets the block default configuration. This configuration determines the block's behavior when a block is initially placed in a region. Default values for the block configuration form should be added to the configuration array. System… |