function DraggableListBuilderTrait::buildForm

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Entity/DraggableListBuilderTrait.php \Drupal\Core\Entity\DraggableListBuilderTrait::buildForm()

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.

See also

\Drupal\Core\Form\FormInterface::buildForm())

4 calls to DraggableListBuilderTrait::buildForm()
FilterFormatListBuilder::buildForm in core/modules/filter/src/FilterFormatListBuilder.php
Form constructor.
LanguageListBuilder::buildForm in core/modules/language/src/LanguageListBuilder.php
Form constructor.
SearchPageListBuilder::buildForm in core/modules/search/src/SearchPageListBuilder.php
Form constructor.
VocabularyListBuilder::buildForm in core/modules/taxonomy/src/VocabularyListBuilder.php
Form constructor.
4 methods override DraggableListBuilderTrait::buildForm()
FilterFormatListBuilder::buildForm in core/modules/filter/src/FilterFormatListBuilder.php
Form constructor.
LanguageListBuilder::buildForm in core/modules/language/src/LanguageListBuilder.php
Form constructor.
SearchPageListBuilder::buildForm in core/modules/search/src/SearchPageListBuilder.php
Form constructor.
VocabularyListBuilder::buildForm in core/modules/taxonomy/src/VocabularyListBuilder.php
Form constructor.

File

core/lib/Drupal/Core/Entity/DraggableListBuilderTrait.php, line 139

Class

DraggableListBuilderTrait
Provides a trait for draggable listings of entities.

Namespace

Drupal\Core\Entity

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    $form[$this->entitiesKey] = [
        '#type' => 'table',
        '#header' => $this->buildHeader(),
        '#empty' => t('There are no @label yet.', [
            '@label' => $this->entityType
                ->getPluralLabel(),
        ]),
        '#tabledrag' => [
            [
                'action' => 'order',
                'relationship' => 'sibling',
                'group' => 'weight',
            ],
        ],
    ];
    $this->entities = $this->load();
    $delta = 10;
    // Change the delta of the weight field if there are more than 20 entities.
    if (!empty($this->weightKey)) {
        $count = count($this->entities);
        if ($count > 20) {
            $delta = ceil($count / 2);
        }
    }
    foreach ($this->entities as $entity) {
        $row = $this->buildRow($entity);
        if (isset($row['label'])) {
            $row['label'] = [
                '#plain_text' => $row['label'],
            ];
        }
        if (isset($row['weight'])) {
            $row['weight']['#delta'] = $delta;
        }
        $form[$this->entitiesKey][$entity->id()] = $row;
    }
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => t('Save'),
        '#button_type' => 'primary',
    ];
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.