Skip to content

Commit

Permalink
Improve various prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
DieterHolvoet committed Nov 24, 2021
1 parent 49b0055 commit 1730859
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/Drupal/Commands/core/FieldCreateCommands.php
Expand Up @@ -22,6 +22,7 @@
use Drupal\field\FieldStorageConfigInterface;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\ChoiceQuestion;

class FieldCreateCommands extends DrushCommands implements CustomEventAwareInterface
{
Expand Down Expand Up @@ -259,7 +260,7 @@ protected function askExistingFieldName(): ?string
return null;
}

return $this->choice('Choose an existing field', $choices);
return $this->io()->choice('Choose an existing field', $choices);
}

protected function askFieldName(): string
Expand All @@ -275,7 +276,7 @@ protected function askFieldName(): string
}

while (!$fieldName) {
$answer = $this->io()->ask('Field name', $machineName);
$answer = $this->io()->ask('Field name', $machineName, [static::class, 'validateRequired']);

if (!preg_match('/^[_a-z]+[_a-z0-9]*$/', $answer)) {
$this->logger()->error('Only lowercase alphanumeric characters and underscores are allowed, and only lowercase letters and underscore are allowed as the first character.');
Expand All @@ -300,12 +301,12 @@ protected function askFieldName(): string

protected function askFieldLabel(): string
{
return $this->io()->ask('Field label');
return $this->io()->ask('Field label', null, [static::class, 'validateRequired']);
}

protected function askFieldDescription(): ?string
{
return $this->optionalAsk('Field description');
return $this->io()->ask('Field description');
}

protected function askFieldType(): string
Expand Down Expand Up @@ -342,7 +343,7 @@ protected function askFieldWidget(): string
$choices[$name] = $label;
}

return $this->io()->choice('Field widget', $choices, 0);
return $this->io()->choice('Field widget', $choices);
}

protected function askRequired(): bool
Expand Down Expand Up @@ -411,7 +412,7 @@ protected function askCardinality(): int
$cardinality = $this->io()->choice(
'Allowed number of values',
array_combine($choices, $choices),
0
1
);

$limit = FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED;
Expand Down Expand Up @@ -453,7 +454,10 @@ protected function askReferencedBundles(FieldDefinitionInterface $fieldDefinitio
$choices[$bundle] = $label;
}

return $this->io()->choice('Referenced bundles', $choices, 0);
$question = (new ChoiceQuestion('Referenced bundles', $choices))
->setMultiselect(true);

return $this->io()->askQuestion($question);
}

protected function createField(): FieldConfigInterface
Expand Down Expand Up @@ -709,10 +713,14 @@ protected function ensureOption(string $name, callable $asker, bool $required):
$this->input->setOption($name, $value);
}

protected function optionalAsk(string $question)
public static function validateRequired(?string $value): string
{
return $this->io()->ask($question, null, function ($value) {
return $value;
});
// FALSE is not considered as empty value because question helper use
// it as negative answer on confirmation questions.
if ($value === NULL || $value === '') {
throw new \UnexpectedValueException('This value is required.');
}

return $value;
}
}

0 comments on commit 1730859

Please sign in to comment.