You are here

public function GridStackVariantSelectionForm::afterBuildSelection in GridStack 8.2

Provides preview classes.

File

modules/gridstack_ui/src/Form/GridStackVariantSelectionForm.php, line 166

Class

GridStackVariantSelectionForm
Provides variant selection form.

Namespace

Drupal\gridstack_ui\Form

Code

public function afterBuildSelection(array $element) {
  $settings = $element['#settings'];
  $removed = [
    'form-wrapper--selection',
  ];
  foreach (Element::children($element) as $key) {
    if ($variant = GridStackVariant::load($key)) {
      if ($uri = $variant
        ->getIconUri()) {
        $token_query = [
          IMAGE_DERIVATIVE_TOKEN => time(),
        ];
        $image_url = \file_url_transform_relative(\file_create_url($uri));
        $image_url .= (strpos($image_url, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($token_query);
        $icon = [
          '#theme' => 'image',
          '#uri' => $image_url,
          '#alt' => $this
            ->t('Thumbnail'),
        ];
        $element[$key]['#field_suffix'] = $this->manager
          ->getRenderer()
          ->render($icon);
      }
    }

    // Unfortunately container classes are also inherited by each item.
    if (isset($element[$key]['#attributes']['class'])) {
      $classes = $element[$key]['#attributes']['class'];
      $classes = array_diff($classes, $removed);
      $element[$key]['#attributes']['class'] = $classes;
    }
    $title = '';
    if ($key == $settings['vid']) {
      $element[$key]['#wrapper_attributes']['class'][] = 'was-gs-selected';
      $title .= $this
        ->t('selected');
    }
    if ($key == $settings['pub']) {
      $element[$key]['#wrapper_attributes']['class'][] = 'was-gs-selected is-gs-applied';
      $title .= $this
        ->t('currently applied');
    }
    if ($key == $settings['pub'] && $key == $settings['vid']) {
      $element[$key]['#wrapper_attributes']['class'][] = 'was-gs-selected is-gs-applied';
      $title = $this
        ->t('selected, currently applied');
    }
    if ($title) {
      $element[$key]['#title'] .= '<br>(' . $title . ')';
    }
  }
  return $element;
}