View source
<?php
namespace Drupal\gridstack_ui\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\gridstack\Entity\GridStackVariant;
use Drupal\gridstack\GridStackDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GridStackVariantFormController extends ControllerBase {
protected $request;
protected $manager;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->request = $container
->get('request_stack')
->getCurrentRequest();
$instance->manager = $container
->get('gridstack.manager');
return $instance;
}
public function add($gridstack, $gid, $gridstack_variant) {
$name = str_replace([
'-',
], '_', $gridstack_variant);
$entity = GridStackVariant::load($name);
if (empty($entity)) {
$values = $gridstack
->toArray();
$options = $values['options'];
$options['icon'] = 'public://gridstack/' . $name . '.png';
$entity = GridStackVariant::create([
'name' => $name,
'label' => str_replace([
'-',
'_',
], ' ', $gridstack_variant),
'options' => $options,
'source' => $gridstack
->id(),
]);
$entity
->set('id', $name);
$entity
->save();
}
return $this
->edit($gridstack, $gid, $entity);
}
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);
}
public function delete($gridstack, $gid, $gridstack_variant) {
$this
->messenger()
->addMessage($this
->t('Variant %label was deleted', [
'%label' => $gridstack_variant
->label(),
]));
$gridstack_variant
->delete();
}
public function edit($gridstack, $gid, $gridstack_variant) {
$name = $this->request->query
->get('dup', NULL);
$label = $this->request->query
->get('label', NULL);
if ($name) {
$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();
}
$gridstack_variant = $entity;
}
$form = $this
->entityFormBuilder()
->getForm($gridstack_variant, 'edit') ?: [];
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('#' . GridStackDefault::variantWrapperId($gid), $form));
return $response;
}
public function cancel($gridstack, $gid) {
$pub = $this->request->query
->get('pub', NULL);
$vid = $this->request->query
->get('vid', NULL);
$id = $gridstack
->id();
$config = [
'gid' => $gid,
'optionset' => $id,
'vid' => $vid,
'pub' => $pub,
];
$editor = $this->manager
->stylizer()
->builder()
->getVariantEditor($config, $gridstack);
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#' . GridStackDefault::variantWrapperId($gid), $editor['form']));
return $response;
}
public function selection($gridstack, $gid) {
$pub = $this->request->query
->get('pub', NULL);
$vid = $this->request->query
->get('vid', NULL);
$id = $gridstack
->id();
$form = $this
->formBuilder()
->getForm('Drupal\\gridstack_ui\\Form\\GridStackVariantSelectionForm', $id, $gid, $vid, $pub);
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('#' . GridStackDefault::variantWrapperId($gid), $form));
return $response;
}
}