public function ProductVariationListBuilder::buildForm in Commerce Core 8.2
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 FormInterface::buildForm
File
- modules/
product/ src/ ProductVariationListBuilder.php, line 174
Class
- ProductVariationListBuilder
- Defines the list builder for product variations.
Namespace
Drupal\commerce_productCode
public function buildForm(array $form, FormStateInterface $form_state) {
$variations = $this
->load();
if (count($variations) <= 1) {
$this->hasTableDrag = FALSE;
}
$delta = 10;
// Dynamically expand the allowed delta based on the number of entities.
$count = count($variations);
if ($count > 20) {
$delta = ceil($count / 2);
}
// Override the page title to contain the product label.
$form['#title'] = $this
->t('%product variations', [
'%product' => $this->product
->label(),
]);
$form['variations'] = [
'#type' => 'table',
'#header' => $this
->buildHeader(),
'#empty' => $this
->t('There are no @label yet.', [
'@label' => $this->entityType
->getPluralLabel(),
]),
];
foreach ($variations as $entity) {
$row = $this
->buildRow($entity);
$row['sku'] = [
'#markup' => $row['sku'],
];
$row['title'] = [
'#markup' => $row['title'],
];
$row['price'] = [
'#type' => 'inline_template',
'#template' => '{{ price|commerce_price_format }}',
'#context' => [
'price' => $row['price'],
],
];
$row['status'] = [
'#markup' => $row['status'],
];
if (isset($row['weight'])) {
$row['weight']['#delta'] = $delta;
}
$form['variations'][$entity
->id()] = $row;
}
if ($this->hasTableDrag) {
$form['variations']['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight',
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
];
}
return $form;
}