LayoutSelect.php in Layout Paragraphs 2.0.x
File
src/Element/LayoutSelect.php
View source
<?php
namespace Drupal\layout_paragraphs\Element;
use Drupal\Core\Render\Element\Radios;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Component\Render\FormattableMarkup;
class LayoutSelect extends Radios {
protected $layoutPluginManager;
public function getInfo() {
$info = parent::getInfo();
$info += [
'#width' => 40,
'#height' => 60,
'#stroke_width' => 1,
'#padding' => 0,
];
$info['#process'][] = [
__CLASS__,
'processLayoutSelect',
];
return $info;
}
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;
}
}