You are here

public function PromotionListBuilder::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/promotion/src/PromotionListBuilder.php, line 202

Class

PromotionListBuilder
Defines the list builder for promotions.

Namespace

Drupal\commerce_promotion

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Start by loading the enabled promotions.
  $this->enabledEntities = $this
    ->load();
  if (count($this->enabledEntities) <= 1) {
    $this->hasTableDrag = FALSE;
  }
  $delta = 10;

  // Dynamically expand the allowed delta based on the number of entities.
  $count = count($this->enabledEntities);
  if ($count > 20) {
    $delta = ceil($count / 2);
  }
  $table_header = $this
    ->buildHeader();
  $form['enabled_promotions'] = [
    '#type' => 'table',
    '#header' => $table_header,
    '#empty' => $this
      ->t('There are no enabled @label yet.', [
      '@label' => $this->entityType
        ->getPluralLabel(),
    ]),
    '#caption' => $this
      ->t('Enabled'),
  ];

  // Only add the pager if a limit is specified.
  if ($this->limit) {
    $form['pager_enabled_promotions'] = [
      '#type' => 'pager',
      '#element' => 0,
    ];
  }

  // Now load the disabled promotions.
  $this->statusCondition = FALSE;
  $this->disabledEntities = $this
    ->load();
  $form['disabled_promotions'] = [
    '#type' => 'table',
    // Table dragging is only enabled for enabled promotions, therefore,
    // removing the "weight" header if present.
    '#header' => array_diff_key($table_header, [
      'weight' => 'weight',
    ]),
    '#empty' => $this
      ->t('There are no disabled @label.', [
      '@label' => $this->entityType
        ->getPluralLabel(),
    ]),
    '#caption' => $this
      ->t('Disabled'),
  ];

  // Only add the pager if a limit is specified.
  if ($this->limit) {
    $form['pager_disabled_promotions'] = [
      '#type' => 'pager',
      '#element' => 1,
    ];
  }
  $entities = array_merge($this->enabledEntities, $this->disabledEntities);
  foreach ($entities as $entity) {
    $row = $this
      ->buildRow($entity);
    $row['name'] = [
      '#markup' => $row['name'],
    ];
    $row['usage'] = [
      '#markup' => $row['usage'],
    ];
    $row['customer_limit'] = [
      '#markup' => $row['customer_limit'],
    ];
    $row['start_date'] = [
      '#markup' => $row['start_date'],
    ];
    $row['end_date'] = [
      '#markup' => $row['end_date'],
    ];
    if (isset($row['weight'])) {
      $row['weight']['#delta'] = $delta;
    }
    if ($entity
      ->isEnabled()) {
      $form['enabled_promotions'][$entity
        ->id()] = $row;
    }
    else {
      $form['disabled_promotions'][$entity
        ->id()] = $row;
    }
  }
  if ($this->hasTableDrag) {
    $form['enabled_promotions']['#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;
}