function ReorderDisplays::buildForm

Same name in other branches
  1. 9 core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php \Drupal\views_ui\Form\Ajax\ReorderDisplays::buildForm()
  2. 8.9.x core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php \Drupal\views_ui\Form\Ajax\ReorderDisplays::buildForm()
  3. 11.x core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php \Drupal\views_ui\Form\Ajax\ReorderDisplays::buildForm()

Overrides FormInterface::buildForm

File

core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php, line 33

Class

ReorderDisplays
Displays the display reorder form.

Namespace

Drupal\views_ui\Form\Ajax

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    
    /** @var \Drupal\views\ViewEntityInterface $view */
    $view = $form_state->get('view');
    $display_id = $form_state->get('display_id');
    $form['#title'] = $this->t('Reorder displays');
    $form['#section'] = 'reorder';
    $form['#action'] = Url::fromRoute('views_ui.form_reorder_displays', [
        'js' => 'nojs',
        'view' => $view->id(),
        'display_id' => $display_id,
    ])
        ->toString();
    $form['view'] = [
        '#type' => 'value',
        '#value' => $view,
    ];
    $displays = $view->get('display');
    $count = count($displays);
    // Sort the displays.
    uasort($displays, function ($display1, $display2) {
        return $display1['position'] <=> $display2['position'];
    });
    $form['displays'] = [
        '#type' => 'table',
        '#id' => 'reorder-displays',
        '#header' => [
            $this->t('Display'),
            $this->t('Weight'),
            $this->t('Remove'),
        ],
        '#empty' => $this->t('No displays available.'),
        '#tabledrag' => [
            [
                'action' => 'order',
                'relationship' => 'sibling',
                'group' => 'weight',
            ],
        ],
        '#tree' => TRUE,
        '#prefix' => '<div class="scroll" data-drupal-views-scroll>',
        '#suffix' => '</div>',
    ];
    foreach ($displays as $id => $display) {
        $form['displays'][$id] = [
            '#display' => $display,
            '#attributes' => [
                'id' => 'display-row-' . $id,
            ],
            '#weight' => $display['position'],
        ];
        // Only make row draggable if it's not the default display.
        if ($id !== 'default') {
            $form['displays'][$id]['#attributes']['class'][] = 'draggable';
        }
        $form['displays'][$id]['title'] = [
            '#markup' => $display['display_title'],
        ];
        $form['displays'][$id]['weight'] = [
            '#type' => 'weight',
            '#value' => $display['position'],
            '#delta' => $count,
            '#title' => $this->t('Weight for @display', [
                '@display' => $display['display_title'],
            ]),
            '#title_display' => 'invisible',
            '#attributes' => [
                'class' => [
                    'weight',
                ],
            ],
        ];
        $form['displays'][$id]['removed'] = [
            'checkbox' => [
                '#title' => $this->t('Remove @id', [
                    '@id' => $id,
                ]),
                '#title_display' => 'invisible',
                '#type' => 'checkbox',
                '#id' => 'display-removed-' . $id,
                '#attributes' => [
                    'class' => [
                        'views-remove-checkbox',
                    ],
                ],
                '#default_value' => !empty($display['deleted']),
            ],
            'link' => [
                '#type' => 'link',
                '#title' => new FormattableMarkup('<span>@text</span>', [
                    '@text' => $this->t('Remove'),
                ]),
                '#url' => Url::fromRoute('<none>'),
                '#attributes' => [
                    'id' => 'display-remove-link-' . $id,
                    'class' => [
                        'views-button-remove',
                        'display-remove-link',
                    ],
                    'alt' => $this->t('Remove this display'),
                    'title' => $this->t('Remove this display'),
                ],
            ],
            '#access' => $id !== 'default',
        ];
        if (!empty($display['deleted'])) {
            $form['displays'][$id]['deleted'] = [
                '#type' => 'value',
                '#value' => TRUE,
            ];
            $form['displays'][$id]['#attributes']['class'][] = 'hidden';
        }
    }
    $view->getStandardButtons($form, $form_state, 'views_ui_reorder_displays_form');
    return $form;
}

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