You are here

public function GridStackVariantSelectionForm::buildForm in GridStack 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

modules/gridstack_ui/src/Form/GridStackVariantSelectionForm.php, line 54

Class

GridStackVariantSelectionForm
Provides variant selection form.

Namespace

Drupal\gridstack_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $args = $form_state
    ->getBuildInfo()['args'];
  $name = $args[0];
  $gid = $args[1];
  $vid = $args[2];
  $pub = $args[3];
  if (!isset($this->optionset)) {
    $this->optionset = GridStack::loadWithFallback($name);
  }
  $variants = $this->manager
    ->stylizer()
    ->builder()
    ->getVariants($name, TRUE);
  $settings = [
    'gid' => $gid,
    'optionset' => $name,
    'vid' => $vid,
    'pub' => $pub,
  ];
  $links = $this->manager
    ->stylizer()
    ->builder()
    ->getVariantLinks($settings, $this->optionset, TRUE);
  $back = empty($links['edit']) ? [] : $links['edit'];
  if ($back) {
    $back['#title'] = $this
      ->t('Back');
  }
  $vid_wrapper_id = 'edit-vid-' . $gid;
  $button_wrapper_id = 'edit-variant-actions-' . $gid;
  $form_wrapper_id = GridStackDefault::variantWrapperId($gid);
  $form['#attributes']['class'][] = 'form form--gridstack-variant-selection';
  $form['container'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'btn-group',
      ],
    ],
    '#prefix' => '<div id="' . $button_wrapper_id . '" class="form--gridstack-variant__actions">',
    '#suffix' => '</div>',
    'back' => $back,
    'delete' => [
      '#type' => 'button',
      '#value' => $this
        ->t('Delete'),
      '#ajax' => [
        'callback' => '::deleteVariant',
        'effect' => 'fade',
        'wrapper' => $form_wrapper_id,
        'method' => 'html',
      ],
      '#attributes' => [
        'class' => [
          'btn',
          'btn-danger',
          'btn--variant-delete',
          'visually-hidden',
        ],
        'data-gs-vid' => '',
        'data-gs-variant-ajax' => 'delete',
        'data-gs-variant-message' => $this
          ->t('Deleting'),
      ],
      '#limit_validation_errors' => [],
    ],
    'edit' => [],
    'cancel' => [
      '#type' => 'button',
      '#value' => $this
        ->t('Cancel'),
      '#ajax' => [
        'callback' => '::cancelSelection',
        'effect' => 'fade',
        'wrapper' => $form_wrapper_id,
      ],
      '#attributes' => [
        'class' => [
          'btn',
          'btn-warning',
          'btn--selection-cancel',
        ],
      ],
    ],
  ];
  $icons = [];
  if ($uri = $this->optionset
    ->getIconUri()) {
    $icons['base'] = [
      '#theme' => 'image',
      '#uri' => $uri,
      '#alt' => $this
        ->t('Thumbnail'),
      '#suffix' => $this
        ->t('@label <br>(original layout)', [
        '@label' => $this->optionset
          ->label(),
      ]),
    ];
  }
  $form['icon'] = [
    '#theme' => 'item_list',
    '#items' => $icons,
    '#wrapper_attributes' => [
      'class' => [
        'item-list--icon',
      ],
    ],
  ];
  $form['vid'] = [
    '#title' => $this
      ->t("Choose/ re-use an existing variant"),
    '#type' => 'radios',
    '#options' => $variants,
    '#default_value' => '',
    '#after_build' => [
      [
        $this,
        'afterBuildSelection',
      ],
    ],
    '#ajax' => [
      'callback' => '::editVariant',
      'wrapper' => $button_wrapper_id,
      'method' => 'replace',
    ],
    '#settings' => $settings,
    '#prefix' => '<div id="' . $vid_wrapper_id . '" class="form-wrapper--selection">',
    '#suffix' => '</div>',
  ];
  return $form;
}