/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $container = $this->getContainer(); $mail_validator = $container->get('email.validator'); $country_repository = $container->get('address.country_repository'); $currency_repository = new CurrencyRepository(); $helper = $this->getHelper('question'); // Symfony Console has no built-in way to ensure the value is not empty. $required_validator = function ($value) { if (empty($value)) { throw new \RuntimeException("Value can't be empty."); } return $value; }; // --name option. $name = $input->getOption('name'); if (!$name) { $question = new Question('Enter the store name: ', ''); $question->setValidator($required_validator); $name = $helper->ask($input, $output, $question); } $input->setOption('name', $name); // --mail option. $mail = $input->getOption('mail'); if (!$mail) { $question = new Question('Enter the store email: ', ''); $question->setValidator(function ($mail) use($mail_validator) { if (empty($mail) || !$mail_validator->isValid($mail)) { throw new \RuntimeException('The entered email is not valid.'); } return $mail; }); $mail = $helper->ask($input, $output, $question); } $input->setOption('mail', $mail); // --country option. $country = $input->getOption('country'); if (!$country) { $country_names = array_flip($country_repository->getList('en')); $question = new Question('Enter the store country: ', ''); $question->setAutocompleterValues($country_names); $question->setValidator($required_validator); $country = $helper->ask($input, $output, $question); $country = $country_names[$country]; } $input->setOption('country', $country); // --currency option. $currency = $input->getOption('currency'); if (!$currency) { $country = $country_repository->get($country, 'en'); $currency_code = $country->getCurrencyCode(); if ($currency_code) { $question = new Question("Enter the store currency [{$currency_code}]: ", $currency_code); } else { $question = new Question('Enter the store currency: '); } $question->setAutocompleterValues(array_keys($currency_repository->getList('en'))); $question->setValidator($required_validator); $currency = $helper->ask($input, $output, $question); } $input->setOption('currency', $currency); }
/** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { $form['kill'] = array('#type' => 'checkbox', '#title' => $this->t('<strong>Delete all products</strong> before generating new.'), '#default_value' => $this->getSetting('kill')); $form['num'] = array('#type' => 'number', '#title' => $this->t('How many products would you like to generate?'), '#default_value' => $this->getSetting('num'), '#required' => TRUE, '#min' => 0); $form['title_length'] = array('#type' => 'number', '#title' => $this->t('Maximum number of characters in titles'), '#default_value' => $this->getSetting('title_length'), '#required' => TRUE, '#min' => 1, '#max' => 255); $form['num_var'] = array('#type' => 'number', '#title' => $this->t('How many variations of products would you like to generate?'), '#default_value' => $this->getSetting('num_var'), '#required' => TRUE, '#min' => 0); $form['title_var_length'] = array('#type' => 'number', '#title' => $this->t("Maximum number of characters in variation's titles"), '#default_value' => $this->getSetting('title_var_length'), '#required' => FALSE, '#min' => 1, '#max' => 255); $form['amount'] = array('#type' => 'fieldset', '#title' => t('Amount')); $form['amount']['price_min'] = array('#type' => 'number', '#title' => $this->t('Minimum of variations price to generate?'), '#default_value' => $this->getSetting('price_min'), '#min' => 0); $form['amount']['price_max'] = array('#type' => 'number', '#title' => $this->t('Maximum of variations price to generate?'), '#default_value' => $this->getSetting('price_max'), '#min' => 0); $currency_repository = new CurrencyRepository(); $options = $currency_repository->getList(); $form['amount']['currency'] = array('#type' => 'select', '#title' => $this->t('Set currency'), '#options' => $options); // We always need a language. $options = array(); $languages = $this->languageManager->getLanguages(LanguageInterface::STATE_ALL); foreach ($languages as $langcode => $language) { $options[$langcode] = $language->getName(); } $form['add_language'] = array('#type' => 'select', '#title' => $this->t('Set language on products'), '#multiple' => TRUE, '#description' => $this->t('Requires locale.module'), '#options' => $options, '#default_value' => array($this->languageManager->getDefaultLanguage()->getId())); $form['#redirect'] = FALSE; return $form; }