You are here

public function BulkFormEntityListBuilder::buildForm in Entity API 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 FormInterface::buildForm

File

src/BulkFormEntityListBuilder.php, line 115

Class

BulkFormEntityListBuilder
Provides a list builder that allows using bulk actions.

Namespace

Drupal\entity

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form[$this->entitiesKey] = [
    '#type' => 'table',
    '#header' => $this
      ->buildHeader(),
    '#empty' => $this
      ->t('There are no @label yet.', [
      '@label' => $this->entityType
        ->getPluralLabel(),
    ]),
    '#tableselect' => TRUE,
    '#attached' => [
      'library' => [
        'core/drupal.tableselect',
      ],
    ],
  ];
  $this->entities = $this
    ->load();
  foreach ($this->entities as $entity) {
    $form[$this->entitiesKey][$entity
      ->id()] = $this
      ->buildRow($entity);
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Apply to selected items'),
    '#button_type' => 'primary',
  ];

  // Ensure a consistent container for filters/operations in the view header.
  $form['header'] = [
    '#type' => 'container',
    '#weight' => -100,
  ];
  $action_options = [];
  foreach ($this->actions as $id => $action) {
    $action_options[$id] = $action
      ->label();
  }
  $form['header']['action'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Action'),
    '#options' => $action_options,
  ];

  // Duplicate the form actions into the action container in the header.
  $form['header']['actions'] = $form['actions'];

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