ExampleConfigurableTextBlock.php
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* @file
* Contains \Drupal\block_example\Plugin\Block\ExampleConfigurableTextBlock.
*/
namespace Drupal\block_example\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* 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().
*
* @Block(
* id = "example_configurable_text",
* admin_label = @Translation("Title of first block (example_configurable_text)")
* )
*/
class ExampleConfigurableTextBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'block_example_string' => $this->t('A default value. This block was created at %time', array('%time' => date('c'))),
);
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['block_example_string_text'] = array(
'#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}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['block_example_string']
= $form_state->getValue('block_example_string_text');
}
/**
* {@inheritdoc}
*/
public function build() {
return array(
'#type' => 'markup',
'#markup' => $this->configuration['block_example_string'],
);
}
}