Skip to content

Commit 6c7ef28

Browse files
committed
Fix #80. Skip question when only 1 bundle exists
1 parent 9afd6d5 commit 6c7ef28

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/Command/Entity/EntityBundleClass.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,34 @@ protected function generate(array &$vars): void {
6363

6464
/** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */
6565
$bundle_info = $this->drupalContext->getContainer()->get('entity_type.bundle.info');
66-
$bundle_choices = \array_map(
66+
$bundles = \array_map(
6767
static fn (array $bundle): string => (string) $bundle['label'],
6868
$bundle_info->getBundleInfo($vars['entity_type_id']),
6969
);
70-
// Prepend an 'All' choice for user's convenience.
71-
$bundle_choices_all = ['' => 'All'] + $bundle_choices;
72-
$vars['bundle_ids'] = $this->choice('Bundles, comma separated', $bundle_choices_all, NULL, TRUE);
73-
if (\in_array('', $vars['bundle_ids'])) {
74-
if (\count($vars['bundle_ids']) >= 2) {
75-
throw new \UnexpectedValueException("'All' may not be combined with other choices.");
70+
71+
// Skip the question when only 1 bundle exists.
72+
if (\count($bundles) === 1) {
73+
$vars['bundle_ids'] = \array_keys($bundles);
74+
}
75+
else {
76+
// Prepend an 'All' choice for user's convenience.
77+
$bundle_choices = ['' => 'All'] + $bundles;
78+
$vars['bundle_ids'] = $this->choice('Bundles, comma separated', $bundle_choices, NULL, TRUE);
79+
if (\in_array('', $vars['bundle_ids'])) {
80+
if (\count($vars['bundle_ids']) >= 2) {
81+
throw new \UnexpectedValueException("'All' may not be combined with other choices.");
82+
}
83+
// Replace 'all' with all bundle IDs.
84+
$vars['bundle_ids'] = \array_keys($bundles);
7685
}
77-
// Replace 'all' with all bundle IDs.
78-
$vars['bundle_ids'] = \array_keys($bundle_choices);
7986
}
8087

8188
$vars['classes'] = [];
8289
$vars['classes_fqn'] = [];
8390
foreach ($vars['bundle_ids'] as $bundle_id) {
8491
$vars['bundle_id'] = $bundle_id;
8592
$vars['class'] = $this->ask(
86-
\sprintf('Class for %s bundle', $bundle_choices[$bundle_id]),
93+
\sprintf('Class for %s bundle', $bundles[$bundle_id]),
8794
'{bundle_id|camelize}Bundle',
8895
);
8996
$vars['class_fqn'] = '\\' . $vars['namespace'] . '\\' . $vars['class'];

0 commit comments

Comments
 (0)