AvatarsImageRadios.php in Avatar Kit 8
File
src/Element/AvatarsImageRadios.php
View source
<?php
namespace Drupal\avatars\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\Radios;
class AvatarsImageRadios extends Radios {
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#process' => [
[
$class,
'processRadios',
],
],
'#theme_wrappers' => [
'radios',
],
'#pre_render' => [
[
$class,
'preRenderCompositeFormElement',
],
],
'#attached' => [
'library' => [
'avatars/avatars.avatars_image_radios',
],
],
];
}
public static function processRadios(&$element, FormStateInterface $form_state, &$complete_form) {
static::setAttributes($element, [
'avatar_preview_radios',
]);
$parent = $element;
$parent['#options'] = [];
foreach ($parent['#thumbs'] as $id => $thumb) {
$parent['#options'][$id] = (string) $thumb['label'];
}
$parent = parent::processRadios($parent, $form_state, $complete_form);
foreach (Element::children($parent) as $key) {
$thumb = $element['#thumbs'][$key];
$element[$key]['#theme'] = 'avatar_preview_radio';
static::setAttributes($element[$key], [
'avatar_preview_radio',
'avatar_preview_radio__' . $key,
]);
if (isset($thumb['uri'])) {
if ($element['#style_name']) {
$element[$key]['image']['#theme'] = 'image_style';
$element[$key]['image']['#style_name'] = $element['#style_name'];
}
else {
$element[$key]['image']['#theme'] = 'image';
}
$element[$key]['image']['#uri'] = $thumb['uri'];
}
$element[$key]['radio'] =& $parent[$key];
$element[$key]['radio']['#parents'][] = 'radio';
}
return $element;
}
}