You are here

public function GridStackFormBase::form in GridStack 8.2

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

1 call to GridStackFormBase::form()
GridStackForm::form in modules/gridstack_ui/src/Form/GridStackForm.php
Gets the actual form array to be built.
1 method overrides GridStackFormBase::form()
GridStackForm::form in modules/gridstack_ui/src/Form/GridStackForm.php
Gets the actual form array to be built.

File

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

Class

GridStackFormBase
Extends base form for gridstack instance configuration form.

Namespace

Drupal\gridstack_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Change page title for the duplicate operation.
  $label = $this->isVariant ? $this
    ->gridStack()
    ->label() : $this->entity
    ->label();
  if ($this->operation == 'duplicate') {
    $form['#title'] = $this
      ->t('<em>Duplicate gridstack optionset</em>: @label', [
      '@label' => $label,
    ]);
    $this->entity = $this->entity
      ->createDuplicate();
  }

  // Change page title for the edit operation.
  if ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('<em>Edit gridstack optionset</em>: @label', [
      '@label' => $this->entity
        ->label(),
    ]);
  }
  $classes = [
    'gridstack',
    'slick',
    'optionset',
    'gridstack--ui',
  ];
  $tooltip = [
    'class' => [
      'form-item--tooltip-bottom',
    ],
  ];
  $this->default = GridStack::load('default');
  $this->isDefault = $this
    ->gridStack()
    ->id() == 'default';
  $this->adminCss = $this->manager
    ->configLoad('admin_css', 'blazy.settings');
  $this->framework = $this->manager
    ->configLoad('framework', 'gridstack.settings');
  $this->useNested = $this->framework && $this
    ->useFramework();
  $this->options = $this->entity
    ->getOptions();
  $this->settings = $settings = $this
    ->gridStack()
    ->getOptions('settings') ?: [];
  $this->jsConfig = $this
    ->jsonify($settings, TRUE);
  $this->cssConfig = $this
    ->jsonify($this
    ->getNestedSettings(), TRUE);
  $this->grids = $this->entity
    ->getLastBreakpoint();
  $this->nestedGrids = $this->entity
    ->getLastBreakpoint('nested');
  $this->html5Ac = $this->manager
    ->configLoad('html5_ac', 'gridstack.settings');
  $js_settings = [
    'breakpoint' => 'lg',
    'optionset' => $this->entity
      ->isNew() ? 'default' : $this->entity
      ->id(),
  ];
  $this->settings['root'] = TRUE;
  $this->settings['display'] = 'main';
  $this->settings['storage'] = '';
  $this->settings['use_framework'] = $this->useNested;
  $this->jsSettings = array_merge($js_settings, $this->settings);

  // Initializes the layout engine.
  $this
    ->initEngine($form);
  $form['#attributes']['class'][] = 'is-gs-nojs';
  $form['#attributes']['class'][] = 'has-tooltip';
  $form['#attributes']['data-icon'] = $this->iconBreakpoint;
  $form['#attributes']['data-gs-html5-ac'] = empty($this->html5Ac) ? 0 : 1;
  foreach ($classes as $class) {
    $form['#attributes']['class'][] = 'form--' . $class;
  }
  if (!$this->entity
    ->isNew()) {
    $form['#attributes']['class'][] = 'form--optionset--' . str_replace('_', '-', $this
      ->gridStack()
      ->id());
  }
  if ($this->adminCss) {
    $form['#attached']['library'][] = 'blazy/admin';
    $form['#attributes']['class'][] = 'form--blazy-on';
  }
  else {
    $form['#attributes']['class'][] = 'form--blazy-off';
  }
  $base_settings = $this->default
    ->getOptions('settings');
  $form['#attached']['library'][] = 'gridstack/admin';
  $form['#attached']['drupalSettings']['gridstack'] = $base_settings;

  // Load all grids to get live preview going, except 12.
  // The 12 column is split into gridstack.library.css + gridstack.static.css.
  foreach (range(1, 11) as $key) {
    $form['#attached']['library'][] = 'gridstack/gridstack.' . $key;
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $this->entity
      ->label(),
    '#maxlength' => 255,
    '#required' => TRUE,
    '#description' => $this
      ->t("Label for the GridStack optionset."),
    '#wrapper_attributes' => $tooltip,
    '#prefix' => '<div class="form__header form__half form__half--first has-tooltip clearfix">',
  ];

  // Keep the legacy CTools ID, i.e.: name as ID.
  $form['name'] = [
    '#type' => 'machine_name',
    '#default_value' => $this->entity
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#wrapper_attributes' => $tooltip,
    '#disabled' => !$this->entity
      ->isNew(),
    '#suffix' => '</div>',
    '#machine_name' => [
      'source' => [
        'label',
      ],
      'exists' => '\\Drupal\\gridstack\\Entity\\GridStack::load',
    ],
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#default_value' => $this->entity
      ->description(),
    '#description' => $this
      ->t("Administrative description."),
    '#wrapper_attributes' => $tooltip,
  ];
  $form['screenshot'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'form--gridstack__screenshot',
      ],
      'id' => 'gridstack-screenshot',
    ],
    '#weight' => 100,
  ];
  $form['canvas'] = [
    '#markup' => '<canvas id="gridstack-canvas"></canvas>',
    '#allowed_tags' => [
      'canvas',
    ],
    '#weight' => 100,
  ];
  $this
    ->jsonForm($form);
  return $form;
}