From b716bd5228872217d5f1e029f885614ef6f76abe Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Fri, 9 Jun 2017 14:55:37 -0400 Subject: [PATCH] Add module machine names to autocomplete for Module label. --- composer.json | 4 ++-- .../generate/Helper/InputPreprocessor.php | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 4ca33623334..bcd3e5dd4fa 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "repositories": [ { "type": "vcs", - "url": "https://github.com/Chi-teck/drupal-code-generator" + "url": "https://github.com/weitzman/drupal-code-generator" } ], "require": { @@ -36,7 +36,7 @@ "league/container": "~2", "consolidation/robo": "~1", "symfony/config": "~2.2|^3", - "chi-teck/drupal-code-generator": "dev-master", + "weitzman/drupal-code-generator": "dev-add-normalizer", "consolidation/annotated-command": "^2.4", "consolidation/output-formatters": "^3.1.10", "symfony/yaml": "~2.3|^3", diff --git a/src/Commands/generate/Helper/InputPreprocessor.php b/src/Commands/generate/Helper/InputPreprocessor.php index d567ad35ed1..d34e1040ce7 100644 --- a/src/Commands/generate/Helper/InputPreprocessor.php +++ b/src/Commands/generate/Helper/InputPreprocessor.php @@ -46,7 +46,6 @@ public function preprocess(array &$questions) // Module related generators. if ($destination == 'modules/%') { $modules = []; - // @todo - For better UX, match on both labels and machine names. $moduleHandler = \Drupal::moduleHandler(); foreach ($moduleHandler->getModuleList() as $machine_name => $module) { $modules[$machine_name] = $moduleHandler->getName($machine_name); @@ -55,7 +54,10 @@ public function preprocess(array &$questions) $questions['machine_name'][3] = array_keys($modules); if (isset($questions['name'])) { - $questions['name'][3] = array_values($modules); + // Set autocomplete values and a normalizer to change from machine_name to label. + $questions['name'][3] = $modules; + $questions['name'][5] = [$this, 'machineToLabel']; + $questions['machine_name'][1] = function ($vars) use ($modules) { $machine_name = array_search($vars['name'], $modules); return $machine_name ?: Utils::human2machine($vars['name']); @@ -71,6 +73,7 @@ public function preprocess(array &$questions) $themes[$machine_name] = $theme->info['name']; } $questions['name'][3] = array_values($themes); + $questions['machine_name'][1] = function ($vars) use ($themes) { $machine_name = array_search($vars['name'], $themes); return $machine_name ?: Utils::human2machine($vars['name']); @@ -78,4 +81,12 @@ public function preprocess(array &$questions) $questions['machine_name'][3] = array_keys($themes); } } + + public function machineToLabel($choice) { + $handler = \Drupal::moduleHandler(); + if ($handler->moduleExists($choice)) { + return $handler->getName($choice); + } + return $choice; + } }