Maak een MyCustomBlock.php bestand aan bij mymodule/src/Plugin/Block
<?php
namespace Drupal\mymodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a custom block.
*
* @Block(
* id = "my_custom_block",
* admin_label = @Translation("My custom block")
* )
*/
class MyCustomBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'my_favorite_drink' => $this->t('My favorite drink'),
);
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['my_favorite_drink'] = array(
'#type' => 'textfield',
'#title' => $this->t('My favorite drink'),
'#description' => $this->t('What would you like to drink?'),
'#default_value' => $this->configuration['my_favorite_drink'],
);
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['my_favorite_drink'] = $form_state->getValue('my_favorite_drink');
}
/**
* {@inheritdoc}
*/
public function build() {
$block = array();
if ($this->configuration['my_favorite_drink']) {
$block['label'] = array(
'#type' => 'markup',
'#markup' => $this->configuration['my_favorite_drink'],
);
}
return $block;
}
}