You are here

public function PageReorderVariantsForm::buildForm in Page Manager 8.4

Same name and namespace in other branches
  1. 8 page_manager_ui/src/Form/PageReorderVariantsForm.php \Drupal\page_manager_ui\Form\PageReorderVariantsForm::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.

Overrides FormInterface::buildForm

File

page_manager_ui/src/Form/PageReorderVariantsForm.php, line 61

Class

PageReorderVariantsForm
Provides a form for adding a variant.

Namespace

Drupal\page_manager_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $machine_name = '') {
  $cached_values = $this->tempstore
    ->get($this
    ->getTempstoreId())
    ->get($machine_name);
  $form_state
    ->setTemporaryValue('wizard', $cached_values);

  /** @var $page \Drupal\page_manager\Entity\Page */
  $page = $cached_values['page'];
  $form['variants'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Label'),
      $this
        ->t('Plugin'),
      $this
        ->t('Weight'),
    ],
    '#empty' => $this
      ->t('There are no variants.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'variant-weight',
      ],
    ],
  ];
  $variants = $page
    ->getVariants();

  // Variants can be resorted, but the getVariants() method is still cached
  // so manually invoke the sorting for this UI.
  @uasort($variants, [
    $page,
    'variantSortHelper',
  ]);
  if (!empty($cached_values['deleted_variants'])) {
    foreach ($cached_values['deleted_variants'] as $page_variant) {
      unset($variants[$page_variant
        ->id()]);
    }
  }

  /** @var \Drupal\page_manager\PageVariantInterface $page_variant */
  foreach ($variants as $page_variant) {
    $row = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
    ];
    $row['label']['#markup'] = $page_variant
      ->label();
    $row['id']['#markup'] = $page_variant
      ->getVariantPlugin()
      ->adminLabel();
    $row['weight'] = [
      '#type' => 'weight',
      '#default_value' => $page_variant
        ->getWeight(),
      '#title' => $this
        ->t('Weight for @page_variant variant', [
        '@page_variant' => $page_variant
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#attributes' => [
        'class' => [
          'variant-weight',
        ],
      ],
    ];
    $form['variants'][$page_variant
      ->id()] = $row;
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Update'),
  ];
  return $form;
}