You are here

function opigno_learning_path_preprocess_multiselect_new_class in Opigno Learning path 8

Implements hook_preprocess_multiselect().

File

./opigno_learning_path.module, line 352
Contains opigno_learning_path.module.

Code

function opigno_learning_path_preprocess_multiselect_new_class(&$variables) {
  $element = $variables['element'];
  Element::setAttributes($element, array(
    'id',
    'name',
    'size',
    'required',
  ));
  $available_options = Multiselect::getOptions('available', $element);
  $available_size = min(count($available_options), 10);
  $selected_options = Multiselect::getOptions('selected', $element);
  $selected_size = min(count($selected_options), 10);
  $total_size = $available_size + $selected_size;
  $variables['multiselect'] = array(
    'available' => array(
      'id' => $element['#attributes']['id'] . '-available',
      'label' => t('Available Options'),
      'attributes' => array(
        'id' => $element['#attributes']['id'] . '-available',
        'size' => $total_size,
      ),
      'options' => $available_options,
    ),
    'selected' => array(
      'id' => $element['#attributes']['id'],
      'label' => t('Selected Options'),
      'attributes' => $element['#attributes'],
      'options' => $selected_options,
    ),
    'labels' => array(
      'add' => t('Add'),
      'remove' => t('Remove'),
    ),
  );

  // Prepare selected attributes.
  $variables['multiselect']['selected']['attributes']['size'] = $total_size;

  // Prepare attributes for available select.
  foreach (array(
    'multiple',
    'required',
    'class',
  ) as $key) {
    $element_key = "#{$key}";
    if (isset($element[$element_key])) {
      $variables['multiselect']['available']['attributes'][$key] = $element[$element_key];
    }
  }

  // Prepare attributes.
  $multiselect =& $variables['multiselect'];
  foreach (array(
    'available',
    'selected',
  ) as $key) {
    $multiselect[$key]['attributes']['class'][] = 'multiselect-' . $key;
    $multiselect[$key]['attributes']['class'][] = 'form-multiselect';
    if (isset($multiselect[$key]['attributes']) && !$multiselect[$key]['attributes'] instanceof Attribute) {
      if ($multiselect[$key]['attributes']) {
        $multiselect[$key]['attributes'] = new Attribute($multiselect[$key]['attributes']);
      }
    }
  }
}