You are here

function _multiselect_build_widget_code in Multiselect 7

Build the widget HTML code.

Parameters

$options = array of options available for use in the widget:

$items = array of previously selected options (or default values in FAPI calls):

$element = the form element being created: return $widget = an array of html items and values for use in the final presentation of widget.

3 calls to _multiselect_build_widget_code()
multiselect_field_widget_form in ./multiselect.module
Implements hook_field_widget_form(). Build the form widget using Form API (as much as possible).
theme_multiselect in ./multiselect.module
Returns HTML for a select form element.
_multiselect_after_build in ./multiselect.module
After_build callback for this widget.

File

./multiselect.module, line 233
Allows users to select multiple items in an easier way than the normal node-reference widget.

Code

function _multiselect_build_widget_code($options, $items, $element, $required = FALSE) {

  // For this specific widget, HTML should be filtered out and entities left unencoded.
  // See content_allowed_values / content_filter_xss / filter_xss.
  _multiselect_allowed_values_filter_html($options);

  // Create some arrays for use later in the function.
  $selected_options = array();
  $unselected_options = array();

  // Add selected items to the array first
  if (is_array($items)) {
    foreach ($items as $key => $value) {
      if (isset($value['value']['value'])) {

        // Field collections have them nested.
        $selected_options[$value['value']['value']] = $value['value']['value'];
      }
      elseif (isset($value['value']['tid'])) {

        // Field collections have them nested. Taxonomy
        $selected_options[$value['value']['tid']] = $value['value']['tid'];
      }
      elseif (isset($value['value']['nid'])) {

        // Field collections have them nested. Node ref
        $selected_options[$value['value']['nid']] = $value['value']['nid'];
      }
      elseif (isset($value['value']['uid'])) {

        // Field collections have them nested. User ref
        $selected_options[$value['value']['uid']] = $value['value']['uid'];
      }
      elseif (isset($value['value'])) {

        // With CCK, it's an array.
        $selected_options[$value['value']] = $value['value'];
      }
      elseif (isset($value['tid'])) {

        // With CCK, it's an array. Taxonomy
        $selected_options[$value['tid']] = $value['tid'];
      }
      elseif (isset($value['nid'])) {

        // With CCK, it's an array. Node ref
        $selected_options[$value['nid']] = $value['nid'];
      }
      elseif (isset($value['uid'])) {

        // With CCK, it's an array. User ref
        $selected_options[$value['uid']] = $value['uid'];
      }
      elseif (isset($value['target_id'])) {

        // Handle Entity Reference.
        $selected_options[$value['target_id']] = $value['target_id'];
      }
      elseif (isset($options[$value])) {

        // With FAPI, it's not.
        $selected_options[$value] = $options[$value];
      }
    }
  }
  else {

    // There's only one selected option.
    if (array_key_exists($items, $options)) {
      $selected_options[$items] = $options[$items];
    }
  }

  // Add the remaining options to the arrays
  foreach ($options as $key => $value) {
    if (!isset($selected_options[$key])) {
      $unselected_options[$key] = $value;

      //$selected_options[$key] = $value;
    }
  }

  // Set up useful variables.
  $selfield = $element['#field_name'] . "_sel";
  $unselfield = $element['#field_name'] . "_unsel";

  // Call methods to create prefix. (ie the non-selected table, etc)
  $prefix_pre = '<div class="form-item multiselect"><label for="edit-title">' . t($element['#title']) . ':';
  if ($required) {
    $prefix_pre .= '<span class="form-required" title="' . t('This field is required.') . '"> * </span>';
  }
  $prefix_pre .= "</label>\n";
  $prefix_pre .= "<div id=\"multiselect_labels" . "_" . $element['#field_name'] . "\" class=\"multiselect_labels\"><div id=\"label_unselected" . "_" . $element['#field_name'] . "\" class=\"label_unselected\">" . t('Available Options') . ":</div>\n";
  $prefix_pre .= "<div id=\"label_selected" . "_" . $element['#field_name'] . "\" class=\"label_selected\">" . t('Selected Options') . ":</div>\n</div>\n";
  $prefix_pre .= "<div id=\"multiselect_available" . "_" . $element['#field_name'] . "\" class=\"multiselect_available\">";
  if (array_key_exists('#size', $element)) {
    $size = $element['#size'];

    // Modules can pass in the size of the select boxes.
  }
  else {
    $size = '10';

    // Default size.
  }
  $prefix_pre .= "<select name=\"" . $unselfield . "\" multiple=\"multiple\" class=\"form-multiselect " . $unselfield . " multiselect_unsel\" id=\"" . $element['#field_name'] . "\" size=\"" . $size . "\">\n";
  $prefix_options = _multiselect_html_for_box_options($unselected_options);
  $prefix_post = "</select>\n</div>\n";
  $prefix_post .= "<ul id=\"multiselect_btns" . "_" . $element['#field_name'] . "\" class=\"multiselect_btns\">\n<li class=\"multiselect_add\" id=\"" . $element['#field_name'] . "\"><a href=\"javascript:;\">Add</a></li>\n<li class=\"multiselect_remove\" id=\"" . $element['#field_name'] . "\"><a href=\"javascript:;\">Remove</a></li>\n</ul>";
  $widget['selected_options'] = $selected_options;
  $widget['unselected_options'] = $unselected_options;
  $widget['selfield'] = $selfield;
  $widget['unselfield'] = $unselfield;
  $widget['prefix_pre'] = $prefix_pre;
  $widget['prefix_options'] = $prefix_options;
  $widget['prefix_post'] = $prefix_post;
  return $widget;
}