public function StockConfigForm::buildForm in Commerce Stock 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ StockConfigForm.php, line 102
Class
- StockConfigForm
- The stock configuration form.
Namespace
Drupal\commerce_stock\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
// Get the default service.
$config = $this
->config('commerce_stock.service_manager');
$default_service_id = $config
->get('default_service_id');
$stock_service_manager = $this->stockServiceManager;
$service_options = $stock_service_manager
->listServiceIds();
$form['service_manager'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Stock services'),
];
$form['service_manager']['default_service_id'] = [
'#type' => 'select',
'#title' => $this
->t('Default service'),
'#options' => $service_options,
'#default_value' => $default_service_id,
];
$form['service_manager']['services'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Services per entity type'),
];
$service_options = array_merge([
'use_default' => $this
->t('- Use default -'),
], $service_options);
foreach ($this->purchasableEntityTypes as $entity_type_id => $entity_type_info) {
$form['service_manager']['services'][$entity_type_id] = [
'#type' => 'fieldset',
'#title' => $entity_type_info['label'],
];
foreach ($entity_type_info['bundles'] as $bundle_id => $bundle_name) {
$config_key = $entity_type_id . '_' . $bundle_id . '_service_id';
$form['service_manager']['services'][$entity_type_id][$config_key] = [
'#type' => 'select',
'#title' => $bundle_name,
'#options' => $service_options,
'#default_value' => $config
->get($config_key) ?: 'use_default',
];
}
}
// Event manager
// Get the default event plugin.
$selected_plugin_id = $config
->get('stock_events_plugin_id') ?: 'core_stock_events';
// Get the list of available plugins.
$type = $this->stockEventsManager;
$plugin_definitions = $type
->getDefinitions();
$plugin_list = [];
foreach ($plugin_definitions as $plugin_definition) {
$id = $plugin_definition['id'];
$description = $plugin_definition['description']
->render();
$plugin_list[$id] = $description;
}
// Select the stock event plugin.
$form['event_manager'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Event Handler'),
];
$form['event_manager']['selected_event_plugin'] = [
'#type' => 'select',
'#title' => $this
->t('Selected plugin'),
'#options' => $plugin_list,
'#default_value' => $selected_plugin_id,
];
// Plugin options to be displayed in a field group.
$form['event_manager']['options'] = [
'#type' => 'vertical_tabs',
'#default_tab' => 'edit-publication',
];
// Cycle the plugins.
foreach ($plugin_definitions as $plugin_definition) {
$id = $plugin_definition['id'];
$description = $plugin_definition['description']
->render();
$plugin_list[$id] = $description;
// Create the form elements for each one.
$form[$id] = [
'#type' => 'details',
'#title' => $description,
'#group' => 'options',
];
$plugin = $type
->createInstance($id);
$event_option = $plugin
->configFormOptions();
$form[$id]['config'] = $event_option;
}
return parent::buildForm($form, $form_state);
}