You are here

public function GridStackVariantForm::duplicateVariant in GridStack 8.2

Callback for duplicating a layout variant.

@fixme, no icon generated, duplicate worflow is not good.

File

modules/gridstack_ui/src/Form/GridStackVariantForm.php, line 368

Class

GridStackVariantForm
Extends base form for gridstack instance configuration form.

Namespace

Drupal\gridstack_ui\Form

Code

public function duplicateVariant(array &$form, FormStateInterface $form_state) {
  $entity = $this->entity;
  $id = $entity
    ->getRandomizedId();
  $form_state
    ->set('new_vid', $id);
  $label = $form_state
    ->getValue('label') ?: $entity
    ->getLabelFromId($id);
  $options = $entity
    ->getOptions();
  unset($options['icon']);
  $clone = $entity
    ->createDuplicateVariant($id, $label, $options);
  $this
    ->setEntity($clone);
  $this->entity
    ->save();
  $id = $this->entity
    ->id();
  $form['variant_actions']['actions']['duplicate']['#attributes']['disabled'] = TRUE;
  $form['label']['#field_suffix'] = $id;
  $this
    ->validateForm($form, $form_state);
  $this
    ->save($form, $form_state);
  $form_wrapper_id = GridStackDefault::variantWrapperId($id);
  $form['#prefix'] = '<div id="' . $form_wrapper_id . '">';
  foreach ([
    'cancel',
    'duplicate',
    'revert',
    'update',
    'apply',
    'save',
  ] as $key) {
    $form['variant_actions']['actions'][$key]['#attributes']['data-gs-vid'] = $id;
    if (!in_array($key, [
      'cancel',
      'update',
    ])) {
      $form['variant_actions']['actions'][$key]['#ajax']['wrapper'] = $form_wrapper_id;
    }
  }
  return $form;
}