You are here

public static function AvatarsImageRadios::processRadios in Avatar Kit 8

Expands a radios element into individual radio elements.

Overrides Radios::processRadios

File

src/Element/AvatarsImageRadios.php, line 59

Class

AvatarsImageRadios
Provides a form element for a set of radio buttons with images.

Namespace

Drupal\avatars\Element

Code

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,
    ]);

    // Image.
    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'];
    }

    // Radio.
    $element[$key]['radio'] =& $parent[$key];
    $element[$key]['radio']['#parents'][] = 'radio';
  }
  return $element;
}