Skip to content

Commit

Permalink
[generate:theme] Add --base-theme-regions option (#4111)
Browse files Browse the repository at this point in the history
* [generate:theme] Add --base-theme-regios option

* [generate:theme] Add validation to get default regions
  • Loading branch information
hjuarez20 authored and enzolutions committed Jul 15, 2019
1 parent 22f114d commit 3bcce31
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/Command/Generate/ThemeCommand.php
Expand Up @@ -159,6 +159,12 @@ protected function configure()
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.theme.options.base-theme')
)
->addOption(
'base-theme-regions',
null,
InputOption::VALUE_NONE,
$this->trans('commands.generate.theme.options.base-theme-regions')
)
->addOption(
'regions',
null,
Expand Down Expand Up @@ -204,6 +210,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$core = $input->getOption('core');
$package = $input->getOption('package');
$base_theme = $input->getOption('base-theme');
$base_theme_regions = $input->getOption('base-theme-regions');
$global_library = $input->getOption('global-library');
$libraries = $input->getOption('libraries');
$regions = $input->getOption('regions');
Expand All @@ -217,6 +224,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$breakpoints = $this->explodeInlineArray($breakpoints);
}

$base_theme_path = $this->extensionManager->getTheme($base_theme);

$this->generator->generate([
'theme' => $theme,
'machine_name' => $machine_name,
Expand All @@ -225,6 +234,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
'description' => $description,
'package' => $package,
'base_theme' => $base_theme,
'base_theme_path' => is_null($base_theme_path) ? false : $base_theme_path->getRealPath(),
'base_theme_regions' => $base_theme_regions,
'global_library' => $global_library,
'libraries' => $libraries,
'regions' => $regions,
Expand All @@ -249,12 +260,11 @@ protected function interact(InputInterface $input, OutputInterface $output)
}

if (!$theme) {
$validators = $this->validator;
$theme = $this->getIo()->ask(
$this->trans('commands.generate.theme.questions.theme'),
'',
function ($theme) use ($validators) {
return $validators->validateModuleName($theme);
function ($theme) {
return $this->validator->validateModuleName($theme);
}
);
$input->setOption('theme', $theme);
Expand All @@ -272,8 +282,8 @@ function ($theme) use ($validators) {
$machine_name = $this->getIo()->ask(
$this->trans('commands.generate.theme.questions.machine-name'),
$this->stringConverter->createMachineName($theme),
function ($machine_name) use ($validators) {
return $validators->validateMachineName($machine_name);
function ($machine_name) {
return $this->validator->validateMachineName($machine_name);
}
);
$input->setOption('machine-name', $machine_name);
Expand Down
7 changes: 7 additions & 0 deletions src/Generator/ThemeGenerator.php
Expand Up @@ -9,6 +9,7 @@

use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Extension\Manager;
use Drupal\Component\Serialization\Yaml;

/**
*
Expand Down Expand Up @@ -71,6 +72,12 @@ public function generate(array $parameters)
}
}

if($parameters['base_theme_regions'] && $parameters['base_theme']) {
$defaultRegions = Yaml::decode(file_get_contents($parameters['base_theme_path']));
$parameters['base_theme_regions'] = $defaultRegions['regions'];
$parameters['base_theme_regions_hidden'] = $defaultRegions['regions_hidden'];
}

$themePath = $dir . '/' . $machine_name;

$this->renderFile(
Expand Down
14 changes: 13 additions & 1 deletion templates/theme/info.yml.twig
Expand Up @@ -14,10 +14,22 @@ base theme: {{ base_theme }}
{% if base_theme == 'classy' %}
#Using Classy as a base theme https://www.drupal.org/theme-guide/8/classy
{% endif %}
{% if regions or base_theme_regions is iterable %}

{% if regions %}
regions:
{% if base_theme_regions is iterable %}
{% for key, default_regions in base_theme_regions %}
{{ key }}: {{ default_regions }}
{% endfor %}
{% endif %}
{% for region in regions %}
{{ region.region_machine_name }}: {{ region.region_name }}
{% endfor %}
{% endif %}
{% if base_theme_regions_hidden is defined and base_theme_regions_hidden is not null %}

regions_hidden:
{% for default_regions_hidden in base_theme_regions_hidden %}
- {{ default_regions_hidden }}
{% endfor %}
{% endif %}

0 comments on commit 3bcce31

Please sign in to comment.