Skip to content
Permalink
Browse files

[generate:plugin:block] Add template option (#4107)

  • Loading branch information...
harold20 authored and enzolutions committed Jul 2, 2019
1 parent aab9529 commit 04f43a75a28ed56a204c8174475a139ded59722c
@@ -154,6 +154,12 @@ protected function configure()
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
$this->trans('commands.common.options.services')
)
->addOption(
'twigtemplate',
null,
InputOption::VALUE_NONE,
$this->trans('commands.generate.plugin.block.options.twigtemplate')
)
->setAliases(['gpb']);
}
@@ -175,6 +181,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$theme_region = $input->getOption('theme-region');
$inputs = $input->getOption('inputs');
$noInteraction = $input->getOption('no-interaction');
$twigTemplate = $input->getOption('twigtemplate');
// Parse nested data.
if ($noInteraction) {
$inputs = $this->explodeInlineArray($inputs);
@@ -204,9 +212,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
'plugin_id' => $plugin_id,
'services' => $build_services,
'inputs' => $inputs,
'twig_template' => $twigTemplate,
]);
$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
if ($theme_region) {
@@ -297,5 +305,14 @@ function ($class) {
$inputs = $this->explodeInlineArray($inputs);
}
$input->setOption('inputs', $inputs);
$twigtemplate = $input->getOption('twigtemplate');
if (!$twigtemplate) {
$twigtemplate = $this->getIo()->confirm(
$this->trans('commands.generate.plugin.block.questions.twigtemplate'),
false
);
$input->setOption('twigtemplate', $twigtemplate);
}
}
}
@@ -36,6 +36,10 @@ public function generate(array $parameters)
$inputs = $parameters['inputs'];
$module = $parameters['module'];
$class_name = $parameters['class_name'];
$twigTemplate = $parameters['twig_template'];
$pluginId = $parameters['plugin_id'];
$parameters['twig_template_name'] = str_replace('_', '-', $pluginId);
$parameters['machine_name'] = $pluginId;
// Consider the type when determining a default value. Figure out what
// the code looks like for the default value tht we need to generate.
@@ -74,5 +78,43 @@ public function generate(array $parameters)
$this->extensionManager->getPluginPath($module, 'Block') . '/' . $class_name . '.php',
$parameters
);
if ($twigTemplate) {
$moduleDirectory = $this->extensionManager->getModule($module)->getPath();
$moduleFilePath = $moduleDirectory . '/' . $module . '.module';
$parameters['file_exist'] = file_exists($moduleFilePath);
$this->renderFile(
'module/module-block-twig-template-append.twig',
$moduleFilePath,
$parameters,
FILE_APPEND
);
$moduleDirectory .= '/templates/';
if (file_exists($moduleDirectory)) {
if (!is_dir($moduleDirectory)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the templates directory as the target directory "%s" exists but is a file.',
realpath($moduleDirectory)
)
);
}
if (!is_writable($moduleDirectory)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the templates directory as the target directory "%s" is not writable.',
realpath($moduleDirectory)
)
);
}
}
$this->renderFile(
'module/templates/plugin-block-html.twig',
$moduleDirectory . $parameters['twig_template_name'] . '.html.twig',
$parameters
);
}
}
}
@@ -0,0 +1,22 @@
{% if file_exist == false %}
<?php
/**
* @file
* module file.
*/
{% endif %}
/**
* Implements hook_theme().
*/
function {{module}}_theme() {
return [
'{{machine_name}}' => [
'variables' => [
'content' => NULL
],
'render element' => 'children',
],
];
}
@@ -135,10 +135,17 @@ class {{class_name}} extends BlockBase {% if services is not empty %}implements
*/
public function build() {
$build = [];
{% if twig_template is defined %}
$build['#theme'] = '{{ plugin_id }}';
{% endif %}
{% for input in inputs %}
{% if twig_template is defined %}
$build['#conten'][] = $this->configuration['{{ input.name }}'];
{% else %}
$build['{{plugin_id}}_{{ input.name }}']['#markup'] = '<p>' . $this->configuration['{{ input.name }}'] . '</p>';
{% endif %}
{% else %}
$build['{{plugin_id}}']['#markup'] = 'Implement {{class_name}}.';
$build['{{plugin_id}}']['#markup'] = 'Implement {{class_name}}.';
{% endfor %}

return $build;
@@ -0,0 +1,18 @@
{{ '{#' }}
/**
* @file
* Default theme implementation to display a block.
*
* @see template_preprocess_block()
*
* @ingroup themeable
*/
{{ '#}' }}
{{ "{% set classes = [
'clear-both',
] %}" }}
<div{{ '{{' }} attributes.addClass(classes) {{ '}}' }}>
{{ '{% for content in value %}' }}
{{ '<p>{{value}}</p>' }}
{{ '{% endfor %}' }}
</div>

0 comments on commit 04f43a7

Please sign in to comment.
You can’t perform that action at this time.