You are here

class GridStackVariantSelectionForm in GridStack 8.2

Provides variant selection form.

Hierarchy

Expanded class hierarchy of GridStackVariantSelectionForm

File

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

Namespace

Drupal\gridstack_ui\Form
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
GridStackVariantSelectionForm::$manager protected property The gridstack manager.
GridStackVariantSelectionForm::$optionset protected property The gridstack optionset.
GridStackVariantSelectionForm::afterBuildSelection public function Provides preview classes.
GridStackVariantSelectionForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
GridStackVariantSelectionForm::cancelSelection public function Callback for cancelling a layout variant.
GridStackVariantSelectionForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
GridStackVariantSelectionForm::deleteVariant public function Callback for deleting a layout variant.
GridStackVariantSelectionForm::editVariant public function Handles switching the available variants based on the selected one.
GridStackVariantSelectionForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
GridStackVariantSelectionForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.