public function PriceListListBuilder::buildForm in Commerce Pricelist 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
- src/
PriceListListBuilder.php, line 145
Class
- PriceListListBuilder
- Defines the list builder for price lists.
Namespace
Drupal\commerce_pricelistCode
public function buildForm(array $form, FormStateInterface $form_state) {
$this->entities = $this
->load();
if (count($this->entities) <= 1) {
$this->hasTableDrag = FALSE;
}
$delta = 10;
// Dynamically expand the allowed delta based on the number of entities.
$count = count($this->entities);
if ($count > 20) {
$delta = ceil($count / 2);
}
$form['price_lists'] = [
'#type' => 'table',
'#header' => $this
->buildHeader(),
'#empty' => $this
->t('There are no @label yet.', [
'@label' => $this->entityType
->getPluralLabel(),
]),
];
foreach ($this->entities as $entity) {
$row = $this
->buildRow($entity);
$row['name'] = [
'#markup' => $row['name'],
];
$row['start_date'] = [
'#markup' => $row['start_date'],
];
$row['end_date'] = [
'#markup' => $row['end_date'],
];
if (isset($row['weight'])) {
$row['weight']['#delta'] = $delta;
}
$form['price_lists'][$entity
->id()] = $row;
}
if ($this->hasTableDrag) {
$form['price_lists']['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'weight',
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#button_type' => 'primary',
];
}
return $form;
}