Search code examples
phpsymfonycommandtypo3typo3-9.x

Select Input TYPO3 v9 Symfony Command


I want to make a scheduler command in TYPO3 v9, the problem is, that I don't know and can't find how I can make a select input.

This is what I have:

/**
 * Basic configuration(s) for the command.
 */
protected function configure(): void
{
    $this->setName('WiRo Workflow')
        ->setDescription('Sendet eine E-Mail an den Redakteur, wenn Inhaltselemente länger als 6 Monate nicht bearbeitet wurden')
        ->addArgument('mailFrom', InputArgument::REQUIRED, 'Absender-Adresse')
        ->addArgument('mailFromName', InputArgument::REQUIRED,'Absender-Name')
        ->addArgument('mailTo', InputArgument::OPTIONAL, 'Empfänger-Adresse (nur für Chefredakteure und Administratoren)')
        ->addOption(
            'colors',
            ['blue', 'red'],
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
            'Which colors do you like?',
            ['blue', 'red']
        )
        ->addOption('optionTV', null, InputOption::VALUE_OPTIONAL, 'Benachrichtigung an: Themenverantwortlicher')
        ->addOption('optionAdmin', null, InputOption::VALUE_NONE, 'admin')
        ->addOption('optionAdminAct', null, InputOption::VALUE_NONE, 'Benachrichtigung an: Chefredakteur und Administratoren (Aktualisierung)')
        ->addArgument('mailTimeToCheck', InputArgument::OPTIONAL, 'Anzahl Monate der Seiten, die zu prüfen sind:')
        ->addArgument('urlMandant', InputArgument::OPTIONAL, 'URL des Mandanten')
        ->addArgument('pageContact', InputArgument::OPTIONAL, 'Themenverantwortliche beziehen aus:')
        ->addArgument('rootPageID', InputArgument::OPTIONAL, 'Root-Page')
        ->addArgument('excludePages', InputArgument::OPTIONAL, 'Auszuschließende Seiten (Komma separierte Liste)')
        ->addArgument('mailSignature', InputArgument::OPTIONAL, 'Mail-Signatur');
}

Solution

  • You cannot put user choice questions on the command options..., instead you have to use a ask method of helper with ChoiceQuestion object to

    • In your commande
    
    use Symfony\Component\Console\Question\ChoiceQuestion;
    
    // ...
    /**
     * Basic configuration(s) for the command.
     */
    protected function configure(): void
    {
        $this->setName('WiRo Workflow')
            ->setDescription('Sendet eine E-Mail an den Redakteur, wenn Inhaltselemente länger als 6 Monate nicht bearbeitet wurden')
            ->addArgument('mailFrom', InputArgument::REQUIRED, 'Absender-Adresse')
            ->addArgument('mailFromName', InputArgument::REQUIRED,'Absender-Name')
            ->addArgument('mailTo', InputArgument::OPTIONAL, 'Empfänger-Adresse (nur für Chefredakteure und Administratoren)')
            ->addOption('optionTV', null, InputOption::VALUE_OPTIONAL, 'Benachrichtigung an: Themenverantwortlicher')
            ->addOption('optionAdmin', null, InputOption::VALUE_NONE, 'admin')
            ->addOption('optionAdminAct', null, InputOption::VALUE_NONE, 'Benachrichtigung an: Chefredakteur und Administratoren (Aktualisierung)')
            ->addArgument('mailTimeToCheck', InputArgument::OPTIONAL, 'Anzahl Monate der Seiten, die zu prüfen sind:')
            ->addArgument('urlMandant', InputArgument::OPTIONAL, 'URL des Mandanten')
            ->addArgument('pageContact', InputArgument::OPTIONAL, 'Themenverantwortliche beziehen aus:')
            ->addArgument('rootPageID', InputArgument::OPTIONAL, 'Root-Page')
            ->addArgument('excludePages', InputArgument::OPTIONAL, 'Auszuschließende Seiten (Komma separierte Liste)')
            ->addArgument('mailSignature', InputArgument::OPTIONAL, 'Mail-Signatur');
    }
    
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        //...
    
        $helper = $this->getHelper('question');
        $question = new ChoiceQuestion(
            'Which colors do you like?',
            ['blue', 'red'],
            0 // default is blue
        );
    
        $question->setErrorMessage('Color %s is invalid.');
    
        $color = $helper->ask($input, $output, $question);
        $output->writeln('You have just selected: ' . $color);
    
        // doSomething with $color
    
    
        return 0;
    }
    //...