Skip to content

Commit 69fb95e

Browse files
dnmurrayjmolivas
authored andcommitted
[#2760] default values for fields in generate:plugin:block (#2761)
1 parent f440499 commit 69fb95e

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/Generator/PluginBlockGenerator.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,42 @@ public function __construct(
3737
*/
3838
public function generate($module, $class_name, $label, $plugin_id, $services, $inputs)
3939
{
40+
// Consider the type when determining a default value. Figure out what
41+
// the code looks like for the default value tht we need to generate.
42+
foreach ($inputs as &$input) {
43+
$default_code = '$this->t(\'\')';
44+
if ($input['default_value'] == '') {
45+
switch ($input['type']) {
46+
case 'checkbox':
47+
case 'number':
48+
case 'weight':
49+
case 'radio':
50+
$default_code = 0;
51+
break;
52+
53+
case 'radios':
54+
case 'checkboxes':
55+
$default_code = 'array()';
56+
break;
57+
}
58+
}
59+
elseif (substr($input['default_value'], 0, 1) == '$') {
60+
// If they want to put in code, let them, they're programmers.
61+
$default_code = $input['default_value'];
62+
}
63+
elseif (is_numeric($input['default_value'])) {
64+
$default_code = $input['default_value'];
65+
}
66+
elseif (preg_match('/^(true|false)$/i', $input['default_value'])) {
67+
// Coding Standards
68+
$default_code = strtoupper($input['default_value']);
69+
}
70+
else {
71+
$default_code = '$this->t(\'' . $input['default_value'] . '\')';
72+
}
73+
$input['default_code'] = $default_code;
74+
}
75+
4076
$parameters = [
4177
'module' => $module,
4278
'class_name' => $class_name,

templates/module/src/Plugin/Block/block.php.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,17 @@ class {{class_name}} extends BlockBase {% if services is not empty %}implements
6969
{% endblock %}
7070
{% block class_methods %}
7171
{% if inputs %}
72-
72+
7373
/**
7474
* {@inheritdoc}
7575
*/
7676
public function defaultConfiguration() {
7777
return [
7878
{% for input in inputs %}
79-
'{{ input.name }}' => $this->t('{{ input.default_value }}'),
79+
'{{ input.name }}' => {{ input.default_code }},
8080
{% endfor %}
8181
] + parent::defaultConfiguration();
82-
82+
8383
}
8484

8585
/**

0 commit comments

Comments
 (0)