You are here

public function ShippingMethodListBuilder::buildForm in Commerce Shipping 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/ShippingMethodListBuilder.php, line 147

Class

ShippingMethodListBuilder
Defines the list builder for shipping methods.

Namespace

Drupal\commerce_shipping

Code

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[$this->entitiesKey] = [
    '#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);
    if (isset($row['name'])) {
      $row['name'] = [
        '#markup' => $row['name'],
      ];
    }
    if (isset($row['status'])) {
      $row['status'] = [
        '#markup' => $row['status'],
      ];
    }
    if (isset($row['weight'])) {
      $row['weight']['#delta'] = $delta;
    }
    $form[$this->entitiesKey][$entity
      ->id()] = $row;
  }
  if ($this->hasTableDrag) {
    $form[$this->entitiesKey]['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'weight',
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Save'),
      '#button_type' => 'primary',
    ];
  }
  return $form;
}