You are here

public function Builder::getVariantLinks in GridStack 8.2

Returns the AJAX CRUD links for layout variants.

1 call to Builder::getVariantLinks()
Builder::getVariantEditor in src/Plugin/gridstack/stylizer/Builder.php
Returns the AJAX CRUD container for layout variants.

File

src/Plugin/gridstack/stylizer/Builder.php, line 255

Class

Builder
Provides Layout Builder integration for editor previews.

Namespace

Drupal\gridstack\Plugin\gridstack\stylizer

Code

public function getVariantLinks(array $settings, $optionset, $reload = FALSE) {
  if (empty($settings['gid']) || empty($settings['optionset'])) {
    return [];
  }
  $links = [];
  $name = $settings['optionset'];
  $vid = isset($settings['vid']) ? $settings['vid'] : '';
  $dup = isset($settings['dup']) ? $settings['dup'] : '';
  $gid = GridStackDefault::gid($settings['gid']);
  $params = [
    'gridstack' => $name,
    'gid' => $gid,
  ];
  $options = [];
  $variant = $vid ? GridStackVariant::load($vid) : NULL;
  $pub = empty($settings['pub']) ? NULL : $settings['pub'];
  $params['gridstack_variant'] = $vid;
  if (!empty($settings['_variant']) && $vid) {
    $pub = $vid;
  }
  if ($dup) {
    $params['dup'] = $dup;
  }
  if ($pub) {
    $params['pub'] = $pub;
  }
  foreach ([
    'add',
    'cancel',
    'delete',
    'duplicate',
    'edit',
    'select',
  ] as $key) {
    $title = $key == 'select' ? 'variants' : $key . ' variant';
    $title = $key == 'cancel' ? $key : $title;
    $links[$key]['#type'] = 'link';
    $links[$key]['#title'] = $this
      ->t('@title', [
      '@title' => ucwords($title),
    ]);
    $links[$key]['#attributes']['class'] = [
      'use-ajax',
      'btn',
      'btn-primary',
      'btn--editor',
      'btn--editor-' . $key,
    ];
  }
  if (empty($variant)) {
    $links['cancel'] = [];
    $links['delete'] = [];
    $links['duplicate'] = [];
    $links['edit'] = [];
    if ($vid) {
      $links['add']['#url'] = new Url('entity.gridstack_variant.ajax_add_form', $params, $options);
    }
  }
  else {

    // @todo $links['add']['#url'] = new Url('entity.gridstack_variant.ajax_add_form', $params, $options);
    $options = [
      'language' => NULL,
      'entity_type' => 'gridstack_variant',
      'entity' => $variant,
    ];
    $params['gridstack_variant'] = $params['vid'] = $variant
      ->id();
    $links['duplicate']['#url'] = new Url('entity.gridstack_variant.ajax_edit_form', $params, $options);
    unset($params['dup'], $params['vid']);
    $links['delete']['#url'] = new Url('entity.gridstack_variant.ajax_delete_form', $params, $options);
    $links['edit']['#url'] = new Url('entity.gridstack_variant.ajax_edit_form', $params, $options);
    $params_cancel = $params;
    $params_cancel['vid'] = $vid;
    unset($params_cancel['gridstack_variant']);
    $links['cancel']['#url'] = new Url('entity.gridstack_variant.ajax_cancel_form', $params_cancel, $options);
  }
  if ($this
    ->getVariants($name, $reload)) {
    $options = [
      'language' => NULL,
      'entity_type' => 'gridstack',
      'entity' => GridStack::loadWithFallback($name),
    ];
    $params['vid'] = $vid;
    unset($params['gridstack_variant']);
    $links['select']['#url'] = new Url('entity.gridstack_variant.ajax_selection_form', $params, $options);
  }
  else {
    $links['select'] = [];
  }
  return $links;
}