class GridStackVariantSelectionForm in GridStack 8.2
Provides variant selection form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\gridstack_ui\Form\GridStackVariantSelectionForm
Expanded class hierarchy of GridStackVariantSelectionForm
File
- modules/
gridstack_ui/ src/ Form/ GridStackVariantSelectionForm.php, line 17
Namespace
Drupal\gridstack_ui\FormView source
class GridStackVariantSelectionForm extends FormBase {
/**
* The gridstack optionset.
*
* @var \Drupal\gridstack\Entity\GridStack
*/
protected $optionset;
/**
* The gridstack manager.
*
* @var \Drupal\gridstack\GridStackManagerInterface
*/
protected $manager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance
->setStringTranslation($container
->get('string_translation'));
$instance->manager = $container
->get('gridstack.manager');
return $instance;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'gridstack_variant_selection_form';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Provides preview classes.
*/
public function afterBuildSelection(array $element) {
$settings = $element['#settings'];
$removed = [
'form-wrapper--selection',
];
foreach (Element::children($element) as $key) {
if ($variant = GridStackVariant::load($key)) {
if ($uri = $variant
->getIconUri()) {
$token_query = [
IMAGE_DERIVATIVE_TOKEN => time(),
];
$image_url = \file_url_transform_relative(\file_create_url($uri));
$image_url .= (strpos($image_url, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($token_query);
$icon = [
'#theme' => 'image',
'#uri' => $image_url,
'#alt' => $this
->t('Thumbnail'),
];
$element[$key]['#field_suffix'] = $this->manager
->getRenderer()
->render($icon);
}
}
// Unfortunately container classes are also inherited by each item.
if (isset($element[$key]['#attributes']['class'])) {
$classes = $element[$key]['#attributes']['class'];
$classes = array_diff($classes, $removed);
$element[$key]['#attributes']['class'] = $classes;
}
$title = '';
if ($key == $settings['vid']) {
$element[$key]['#wrapper_attributes']['class'][] = 'was-gs-selected';
$title .= $this
->t('selected');
}
if ($key == $settings['pub']) {
$element[$key]['#wrapper_attributes']['class'][] = 'was-gs-selected is-gs-applied';
$title .= $this
->t('currently applied');
}
if ($key == $settings['pub'] && $key == $settings['vid']) {
$element[$key]['#wrapper_attributes']['class'][] = 'was-gs-selected is-gs-applied';
$title = $this
->t('selected, currently applied');
}
if ($title) {
$element[$key]['#title'] .= '<br>(' . $title . ')';
}
}
return $element;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// @todo pass vid to layout_settings.
}
/**
* Callback for cancelling a layout variant.
*/
public function cancelSelection(array $form, FormStateInterface $form_state) {
$config = $form['vid']['#settings'];
$gridstack = $this->optionset;
$editor = $this->manager
->stylizer()
->builder()
->getVariantEditor($config, $gridstack);
return $editor['form'];
}
/**
* Callback for deleting a layout variant.
*/
public function deleteVariant(array &$form, FormStateInterface $form_state) {
$vid = $form_state
->getValue('vid');
if ($vid && ($variant = GridStackVariant::load($vid))) {
$variant
->delete();
}
return $form;
}
/**
* Handles switching the available variants based on the selected one.
*/
public function editVariant(array &$form, FormStateInterface $form_state) {
$element = $form['container'];
$settings = $form['vid']['#settings'];
$vid = $settings['vid'] = $form_state
->getValue('vid');
if ($vid && GridStackVariant::load($vid)) {
$links = $this->manager
->stylizer()
->builder()
->getVariantLinks($settings, $this->optionset, TRUE);
if (!empty($links['edit'])) {
$element['edit'] = $links['edit'];
// Cannot use #access for this.
$classes = $element['delete']['#attributes']['class'];
$classes = array_diff($classes, [
'visually-hidden',
]);
$element['delete']['#attributes']['class'] = $classes;
}
}
else {
$element['back'] = [];
}
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
GridStackVariantSelectionForm:: |
protected | property | The gridstack manager. | |
GridStackVariantSelectionForm:: |
protected | property | The gridstack optionset. | |
GridStackVariantSelectionForm:: |
public | function | Provides preview classes. | |
GridStackVariantSelectionForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
GridStackVariantSelectionForm:: |
public | function | Callback for cancelling a layout variant. | |
GridStackVariantSelectionForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
GridStackVariantSelectionForm:: |
public | function | Callback for deleting a layout variant. | |
GridStackVariantSelectionForm:: |
public | function | Handles switching the available variants based on the selected one. | |
GridStackVariantSelectionForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
GridStackVariantSelectionForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |