You are here

public function GridStackForm::jsonify in GridStack 8

Convert the config into a JSON object to reduce logic at frontend.

2 calls to GridStackForm::jsonify()
GridStackForm::form in modules/gridstack_ui/src/Form/GridStackForm.php
Gets the actual form array to be built.
GridStackForm::validateForm in modules/gridstack_ui/src/Form/GridStackForm.php
Form validation handler.

File

modules/gridstack_ui/src/Form/GridStackForm.php, line 632

Class

GridStackForm
Extends base form for gridstack instance configuration form.

Namespace

Drupal\gridstack_ui\Form

Code

public function jsonify($options = [], $preview = FALSE) {
  if (empty($options)) {
    return '';
  }
  $json = [];
  $default = GridStack::load('default')
    ->getOptions('settings');
  $cellHeight = $options['cellHeight'];
  $excludes = [
    'disableDrag',
    'disableResize',
    'draggable',
    'resizable',
    'staticGrid',
  ];
  if (!empty($options)) {
    foreach ($options as $name => $value) {
      if (isset($options[$name]) && !is_array($options[$name])) {
        if (isset($options['noMargin'])) {
          unset($options['noMargin']);
        }
        if (isset($options['width']) && $options['width'] == 12) {
          unset($options['width']);
        }
        if (!in_array($name, [
          'cellHeight',
          'rtl',
        ]) && isset($default[$name])) {
          $cast = gettype($default[$name]);
          settype($options[$name], $cast);
        }
        $json[$name] = $options[$name];
        $json['cellHeight'] = $cellHeight === -1 ? 'auto' : (int) $cellHeight;
        if (empty($options['rtl'])) {
          unset($json['rtl']);
        }
      }

      // Be sure frontend options do not break admin preview.
      if ($preview && in_array($name, $excludes)) {
        unset($json[$name]);
      }
    }
  }
  return Json::encode($json);
}