You are here

function form_process_icon_selector in Icon API 8

Same name and namespace in other branches
  1. 7 includes/element.inc \form_process_icon_selector()

Processes an icon select list form element.

1 string reference to 'form_process_icon_selector'
icon_element_info in includes/element.inc
Implements hook_element_info().

File

includes/element.inc, line 53
element.inc Provides hooks and functions for element related tasks.

Code

function form_process_icon_selector($element) {

  // Get default values.
  $default_bundle = isset($element['#default_bundle']) ? $element['#default_bundle'] : icon_bundle_defaults();
  if (!is_array($default_bundle)) {
    $default_bundle = icon_bundle_load($default_bundle);
  }
  $default_icon = isset($element['#default_icon']) ? $element['#default_icon'] : '';
  $default_value = '';
  if ($default_bundle && !empty($default_bundle['name']) && !empty($default_icon)) {
    $default_value = $default_bundle['name'] . '|' . $default_icon;
  }

  // Build the options array for available icons per bundle.
  $options = array();
  foreach (icon_bundles() as $bundle_name => $bundle) {
    if (!$bundle['status']) {
      continue;
    }
    foreach ($bundle['icons'] as $icon_key => $icon_value) {
      $icon_name = is_string($icon_key) ? $icon_key : $icon_value;
      if (is_array($icon_value) && isset($icon_value['name'])) {
        $icon_name = $icon_value['name'];
      }
      $icon_title = is_string($icon_value) ? $icon_value : $icon_name;
      if (is_array($icon_value) && isset($icon_value['title'])) {
        $icon_title = $icon_value['title'];
      }
      $options[$bundle['title']][$bundle['name'] . '|' . $icon_name] = $icon_title;
    }
  }
  if (!count($options)) {
    $element['#description'] = t('There are no icons available to choose from. They either do not exist or are disabled. As a result, this icon selector has also been disable.');

    // @FIXME
    // l() expects a Url object, created from a route name or external URI.
    // $element['#description'] .= '<br />' . l(t('Manage icon bundles'), ICON_ADMIN_PATH);
    $element['#disabled'] = TRUE;

    // Set the default value.
    if (!empty($default_value)) {
      $options = array(
        $default_value => $default_icon,
      );
    }
  }
  $element['icon'] = array(
    '#title' => t('Select Icon'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : $default_value,
    '#empty_option' => '- No Icon -',
    '#required' => !empty($element['#required']),
    '#weight' => -10,
  );
  $default_wrapper = isset($element['#default_wrapper']) ? $element['#default_wrapper'] : '';
  $element['wrapper'] = array(
    '#type' => 'select',
    '#title' => t('Icon Wrapper'),
    '#description' => t('Choose an HTML element to wrap the icon with.'),
    '#options' => icon_wrapper_options(),
    '#default_value' => $default_wrapper,
    '#states' => array(
      'invisible' => array(
        _icon_states_selector('icon', $element) => array(
          'value' => '',
        ),
      ),
    ),
    '#weight' => -9,
  );
  $default_wrapper_class = isset($element['#default_wrapper_class']) ? $element['#default_wrapper_class'] : '';
  $element['wrapper_class'] = array(
    '#type' => 'textfield',
    '#title' => t('Icon Wrapper Classes'),
    '#description' => t('A space separated list of CSS classes. Token replacement patterns are allowed.'),
    '#default_value' => $default_wrapper_class,
    '#size' => 60,
    '#maxlength' => 128,
    '#states' => array(
      'invisible' => array(
        array(
          _icon_states_selector('icon', $element) => array(
            'value' => '',
          ),
        ),
        array(
          _icon_states_selector('wrapper', $element) => array(
            'value' => '',
          ),
        ),
      ),
    ),
    '#weight' => -8,
  );
  $element['wrapper_class_token'] = array(
    '#type' => 'container',
    '#states' => array(
      'invisible' => array(
        array(
          _icon_states_selector('icon', $element) => array(
            'value' => '',
          ),
        ),
        array(
          _icon_states_selector('wrapper', $element) => array(
            'value' => '',
          ),
        ),
      ),
    ),
    '#weight' => -7,
  );
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $element['wrapper_class_token']['help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => isset($element['#token_types']) ? $element['#token_types'] : 'all',
      '#global_types' => FALSE,
      '#dialog' => TRUE,
    );
  }
  else {
    $element['wrapper_class_token']['help'] = array(
      '#type' => 'item',
      '#markup' => t('Install the <a href="http://drupal.org/project/token" target="_blank" title="Token module project page">Token</a> module to view available replacement patterns.'),
    );
  }
  return $element;
}