<?php

/**
 * @file
 * Contains dropdown.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\Html;

/**
 * Implements hook_help().
 */
function dropdown_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the dropdown module.
    case 'help.page.dropdown':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Dropdown field.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function dropdown_theme() {
  $items = [
    'dropdown' => [
      'render element' => 'element',
    ],
  ];
  return $items;
}

/**
 * Prepares variables for dropdown templates.
 *
 * Default template: dropdown.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 *     Properties used: #title, #value, #options, #description, #required,
 *     #attributes, #children.
 */
function template_preprocess_dropdown(array &$variables) {
  $element = $variables['element'];
  $variables['attributes'] = [];
  if (isset($element['#id'])) {
    $variables['attributes']['id'] = $element['#id'];
  }
  if (isset($element['#attributes']['title'])) {
    $variables['attributes']['title'] = $element['#attributes']['title'];
  }

  $items = [];
  foreach ($element as $key => $el) {
    if (is_numeric($key) && isset($el['#type']) && $el['#type'] === 'radio') {
      $items[$key] = $el;
      if ($el['#return_value'] === $el['#value']) {
        // @todo Do we need to check for default_value also
        $selected = Html::escape($el['#title']);
        $active = $key;
      }
    }
  }

  if (isset($element['#edit_mode'])) {
    $variables['edit_mode'] = $element['#edit_mode'];
  }
  $variables['items'] = $items;
  $variables['label'] = Html::escape($element['#title']);
  $variables['selected'] = $selected ?? $variables['label'];
  $variables['active'] = $active ?? NULL;
  $variables['children'] = $element['#children'];
}
