You are here

function _multiselect_after_build in Multiselect 7

After_build callback for this widget.

1 string reference to '_multiselect_after_build'
multiselect_field_widget_form in ./multiselect.module
Implements hook_field_widget_form(). Build the form widget using Form API (as much as possible).

File

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

Code

function _multiselect_after_build($element, $form_state) {
  $value_key = $element['#columns'][0];
  $items = array();
  if (isset($form_state['values'][$element['#field_name']][LANGUAGE_NONE])) {
    foreach ($form_state['values'][$element['#field_name']][LANGUAGE_NONE] as $nid) {
      $items[] = array(
        $value_key => $nid,
      );
    }
  }
  else {
    if (module_exists('field_collection')) {
      $field_info = field_info_field($element['#field_name']);
      $bundle = $field_info['bundles']['field_collection_item'][0];
      foreach ($form_state['values'][$bundle][LANGUAGE_NONE] as $field) {
        $field_name = $element['#field_name'];
        if (is_array($field) && isset($field[$field_name])) {
          foreach ($field[$field_name][LANGUAGE_NONE] as $nid) {
            $items[] = array(
              $value_key => $nid,
            );
          }
        }
      }
    }
  }
  $widget = _multiselect_build_widget_code($element['#options'], $items, $element, $element['#required']);
  $element['#prefix'] = $widget['prefix_pre'] . $widget['prefix_options'] . $widget['prefix_post'];
  return $element;
}