You are here

protected function GridStackFormBase::jsonify in GridStack 8.2

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

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

File

modules/gridstack_ui/src/Form/GridStackFormBase.php, line 731

Class

GridStackFormBase
Extends base form for gridstack instance configuration form.

Namespace

Drupal\gridstack_ui\Form

Code

protected function jsonify(array $options = [], $preview = FALSE) {
  if (empty($options)) {
    return '';
  }
  $json = [];
  $default = GridStack::load('default')
    ->getOptions('settings');
  $cellHeight = $options['cellHeight'];
  $excludes = [
    'auto',
    'column',
    'float',
    'rtl',
    'minWidth',
    // @todo remove post gridstack_update_8214.
    // @todo 'resizable',
    'disableResize',
    'staticGrid',
    'draggable',
    'disableDrag',
  ];
  if (isset($options['column']) && $options['column'] == 12) {
    unset($options['column']);
  }
  foreach ($options as $name => $value) {

    // @todo unset($options['noMargin']);
    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]);
    }
  }
  if ($preview) {

    // Do not set resizable here, will do it at JS with array options.
    $json['cellHeight'] = $cellHeight == -1 ? 60 : (int) $cellHeight;
  }
  return Json::encode($json);
}