-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAluminumBlockBase.php
More file actions
128 lines (102 loc) · 2.81 KB
/
AluminumBlockBase.php
File metadata and controls
128 lines (102 loc) · 2.81 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Drupal\aluminum_blocks\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Base class for AluminumBlocks.
*
* Created by PhpStorm.
* User: BMcClure.
* Date: 9/9/2016.
* Time: 2:44 PM.
*/
abstract class AluminumBlockBase extends BlockBase implements BlockPluginInterface {
/**
* Optionally override this to manually set an aluminum_id for this block.
*
* @var string
*/
protected $aluminumId = '';
/**
* Override to specify configuration options.
*
* @return array
* An array of options.
*/
public function getOptions() {
return [];
}
/**
* Get the aluminum id.
*
* @return string
* The aluminum id.
*/
public function getAluminumId(): string {
if (!empty($this->aluminumId)) {
return $this->aluminumId;
}
return strtolower(preg_replace([
'/([a-z\d])([A-Z])/',
'/([^_])([A-Z][a-z])/',
], '$1_$2', self::class));
}
/**
* Gets the current value for an option returned by getOptions()
*
* @param string $option_name
* The option name.
* @param bool $replace_tokens
* Whether or not to replace tokens.
*
* @return string
* The option value.
*/
public function getOptionValue(string $option_name, bool $replace_tokens = FALSE): string {
$config = $this->getConfiguration();
$options = $this->getOptions();
$default = $options[$option_name]['#default_value'] ?? '';
$value = $config[$option_name] ?? $default;
if ($replace_tokens) {
$value = \Drupal::token()->replace($value);
}
return $value;
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
foreach ($this->getOptions() as $option_name => $option) {
$option += [
'#type' => 'textfield',
'#title' => ucfirst(str_replace('_', ' ', $option_name)),
];
$default = $option['#default_value'] ?? '';
$option['#default_value'] = $config[$option_name] ?? $default;
$form[$option_name] = $option;
}
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
foreach ($this->getOptions() as $option_name => $option) {
$this->setConfigurationValue($option_name, $form_state->getValue($option_name));
}
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$default_config = \Drupal::config('aluminum_blocks.settings');
$values = [];
foreach ($this->getOptions() as $option_name => $option) {
$values[$option_name] = $default_config->get($this->getAluminumId() . '.' . $option_name);
}
return $values;
}
}