You are here

public function GridStackVariantFormController::duplicate in GridStack 8.2

Duplicates a gridstack variant.

1 string reference to 'GridStackVariantFormController::duplicate'
gridstack_ui.routing.yml in modules/gridstack_ui/gridstack_ui.routing.yml
modules/gridstack_ui/gridstack_ui.routing.yml

File

modules/gridstack_ui/src/Controller/GridStackVariantFormController.php, line 71

Class

GridStackVariantFormController
Provides controller to load GridStackVariantForm.

Namespace

Drupal\gridstack_ui\Controller

Code

public function duplicate($gridstack, $gid, $gridstack_variant) {
  $name = $this->request->query
    ->get('dup', NULL);
  $label = $this->request->query
    ->get('label', NULL);
  if (empty($name)) {
    return [];
  }
  $values = $gridstack_variant
    ->toArray();
  $options = $values['options'];
  $options['icon'] = 'public://gridstack/' . $name . '.png';
  $entity = GridStackVariant::load($name);
  if (empty($entity)) {
    $entity = GridStackVariant::create([
      'name' => $name,
      'label' => $label ?: str_replace([
        '-',
        '_',
      ], ' ', $name),
      'options' => $options,
      'source' => $gridstack
        ->id(),
    ]);
    $entity
      ->set('id', $name);
    $entity
      ->save();
  }
  return $this
    ->edit($gridstack, $gid, $entity);
}