Skip to content

Commit

Permalink
[#2760] default values for fields in generate:plugin:block (#2761)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnmurray authored and jmolivas committed Oct 15, 2016
1 parent f440499 commit 69fb95e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
36 changes: 36 additions & 0 deletions src/Generator/PluginBlockGenerator.php
Expand Up @@ -37,6 +37,42 @@ public function __construct(
*/
public function generate($module, $class_name, $label, $plugin_id, $services, $inputs)
{
// Consider the type when determining a default value. Figure out what
// the code looks like for the default value tht we need to generate.
foreach ($inputs as &$input) {
$default_code = '$this->t(\'\')';
if ($input['default_value'] == '') {
switch ($input['type']) {
case 'checkbox':
case 'number':
case 'weight':
case 'radio':
$default_code = 0;
break;

case 'radios':
case 'checkboxes':
$default_code = 'array()';
break;
}
}
elseif (substr($input['default_value'], 0, 1) == '$') {
// If they want to put in code, let them, they're programmers.
$default_code = $input['default_value'];
}
elseif (is_numeric($input['default_value'])) {
$default_code = $input['default_value'];
}
elseif (preg_match('/^(true|false)$/i', $input['default_value'])) {
// Coding Standards
$default_code = strtoupper($input['default_value']);
}
else {
$default_code = '$this->t(\'' . $input['default_value'] . '\')';
}
$input['default_code'] = $default_code;
}

$parameters = [
'module' => $module,
'class_name' => $class_name,
Expand Down
6 changes: 3 additions & 3 deletions templates/module/src/Plugin/Block/block.php.twig
Expand Up @@ -69,17 +69,17 @@ class {{class_name}} extends BlockBase {% if services is not empty %}implements
{% endblock %}
{% block class_methods %}
{% if inputs %}

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
{% for input in inputs %}
'{{ input.name }}' => $this->t('{{ input.default_value }}'),
'{{ input.name }}' => {{ input.default_code }},
{% endfor %}
] + parent::defaultConfiguration();

}

/**
Expand Down

0 comments on commit 69fb95e

Please sign in to comment.