ProductFeatureAddForm.php in Ubercart 8.4
File
uc_product/src/Form/ProductFeatureAddForm.php
View source
<?php
namespace Drupal\uc_product\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProductFeatureAddForm extends FormBase {
protected $moduleHandler;
public function __construct(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'));
}
public function getFormId() {
return 'uc_product_feature_add_form';
}
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
$form['#node'] = $node;
foreach ($this->moduleHandler
->invokeAll('uc_product_feature') as $feature) {
$options[$feature['id']] = $feature['title'];
}
if (isset($options)) {
ksort($options);
$form['feature'] = [
'#type' => 'select',
'#title' => $this
->t('Add a new feature'),
'#options' => $options,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Add'),
];
}
else {
$form['feature'] = [
'#markup' => $this
->t('There are no product features are available to add. To add a feature you must first enable the module that provides that feature.'),
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->setRedirect('uc_product.feature_add', [
'node' => $form['#node']
->id(),
'fid' => $form_state
->getValue('feature'),
]);
}
}