You are here

protected function Form::globalForm in GridStack 8.2

1 call to Form::globalForm()
Form::buildConfigurationForm in src/Plugin/gridstack/stylizer/Form.php

File

src/Plugin/gridstack/stylizer/Form.php, line 242

Class

Form
Provides the form elements.

Namespace

Drupal\gridstack\Plugin\gridstack\stylizer

Code

protected function globalForm($optionset, FormStateInterface $form_state, array $settings, array $extras = []) {
  $element = [];
  $field_options = $extras['field_options'];
  $framework = $optionset
    ->isFramework();

  // Provides unique variant ID.
  $vid = $this
    ->getVariantUniqueId($optionset);
  if (empty($settings['vid']) && GridStackVariant::load($vid)) {
    $vid = $this
      ->getVariantUniqueId($optionset);
  }
  $element['global'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#title' => $this
      ->t('Main settings'),
  ];
  if (!$this
    ->config('skinless')) {
    $element['global']['skin'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Skin'),
      '#options' => $this->manager
        ->skinManager()
        ->getSkinOptions(),
      '#empty_option' => $this
        ->t('- None -'),
      '#default_value' => $settings['skin'],
    ];
  }
  $empty_desc = $field_options ? '' : ' ' . $this
    ->t('Create one unlimited multi-value Media if none exists.');
  $element['global']['field_name'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Media field'),
    '#options' => $field_options,
    '#empty_option' => $this
      ->t('- None -'),
    '#description' => $this
      ->t('Unlimited <code>Media</code> field to select media from.') . $empty_desc,
    '#default_value' => $settings['field_name'],
  ];
  $element['global']['vm'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Vertical margin'),
    '#options' => GridStackDefault::breakpoints(),
    '#empty_option' => $this
      ->t('- None -'),
    '#description' => $this
      ->t('Avoid overlapping regions globally, unless desired. If bad, use region settings instead.'),
    '#default_value' => $settings['vm'],
    '#access' => !empty($framework),
    '#attributes' => [
      'class' => [
        'form-item--vm',
      ],
    ],
  ];
  $element['global']['gridnative'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use native CSS Grid'),
    '#description' => $this
      ->t('<b>Experimental!</b> Check to replace any js-driven (gridstack, masonry, packery, isotope) layouts with native browser Grid layout. Check out <a href=":url">CSS Grid browser supports</a> relevant to your visitors. Uncheck if any issue.', [
      ':url' => 'https://caniuse.com/#feat=css-grid',
    ]),
    '#default_value' => $settings['gridnative'],
    '#access' => empty($framework),
  ];
  $icons = [];
  $element['global']['icon']['#theme'] = 'item_list';
  $element['global']['icon']['#wrapper_attributes']['class'][] = 'item-list--icon';
  if ($uri = $optionset
    ->getIconUri()) {
    $icons['base'] = [
      '#theme' => 'image',
      '#uri' => $uri,
      '#alt' => $this
        ->t('Thumbnail'),
    ];
  }
  if (!empty($settings['vid']) && ($variant = GridStackVariant::load($settings['vid']))) {
    if ($uri = $variant
      ->getIconUri()) {
      $icons['base']['#suffix'] = $this
        ->t('Original layout');
      $icons['variant'] = [
        '#theme' => 'image',
        '#uri' => $uri,
        '#alt' => $this
          ->t('Thumbnail'),
        '#suffix' => $this
          ->t('Variant @label', [
          '@label' => $variant
            ->label(),
        ]),
      ];
      $element['global']['icon']['#attributes']['class'][] = 'form-wrapper--icon';
    }
  }
  $element['global']['icon']['#items'] = $icons;

  // GridStack variant ID dynamically changed based on selection.
  $element['global']['vid'] = [
    '#type' => 'hidden',
    '#default_value' => empty($settings['vid']) ? $vid : $settings['vid'],
  ];

  // GridStack unique ID for the current layout which can be many on a page.
  $element['global']['gid'] = [
    '#type' => 'hidden',
    '#default_value' => empty($settings['gid']) ? $optionset
      ->id() . ':' . $optionset
      ->randomize(4) : $settings['gid'],
  ];

  // Provides entity-related metadata.
  $entity = $extras;
  unset($entity['entity'], $entity['field_options']);
  $element['global']['metadata'] = [
    '#type' => 'hidden',
    '#default_value' => $extras ? Json::encode($entity) : '',
  ];
  return $element;
}