You are here

public static function LayoutSelect::processLayoutSelect in Layout Paragraphs 2.0.x

Add layout thumbnail previews.

File

src/Element/LayoutSelect.php, line 54

Class

LayoutSelect
Provides a layout selection element.

Namespace

Drupal\layout_paragraphs\Element

Code

public static function processLayoutSelect(&$element, FormStateInterface $form_state, &$complete_form) {
  foreach (Element::children($element) as $key) {
    $layout_name = $key;
    $definition = \Drupal::service('plugin.manager.core.layout')
      ->getDefinition($layout_name);
    $icon = $definition
      ->getIcon($element['#width'], $element['#height'], $element['#stroke_width'], $element['#padding']);
    $rendered_icon = \Drupal::service('renderer')
      ->render($icon);
    $element[$key]['#icon'] = $icon;
    $title = new FormattableMarkup('<span class="layout-select__item-icon">@icon</span><span class="layout-select__item-title">@title</span>', [
      '@title' => $element[$key]['#title'],
      '@icon' => $rendered_icon,
    ]);
    $element[$key]['#title'] = $title;
    $element[$key]['#wrapper_attributes']['class'][] = 'layout-select__item';
    $element[$key]['#attributes']['class'][] = 'visually-hidden';
  }
  $element['#attached']['library'][] = 'layout_paragraphs/layout_select';
  $element['#wrapper_attributes'] = [
    'class' => [
      'layout-select',
    ],
  ];
  return $element;
}