I get "Warning: Illegal offset type in _multiselect_build_widget_code on line 246":

$selected_options[$value['target_id']] = $value['target_id'];

Explicitly casting the value to a string before using it as the key fixes the problem:

$selected_options[(string)$value['target_id']] = $value['target_id'];
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DrCord’s picture

After further testing this appears to be due to ajax happening on an ubercart order page that uses the multiselect widget for an added field. The target id is NULL when the multiselect list selected items are empty.

I realized a better solution would be to never enter the control structure with a weird value that will cause issues, but instead to set the array to an expected input value if it is weird...

so adding these lines at the first line of the function _multiselect_build_widget_code seems to be a better solution:

if( isset($items[0]['target_id']) && $items[0]['target_id']['target_id'] == NULL ){
    $items = array();
  }
DrCord’s picture

After further testing, during ajax calls when the values are set they are returned nested too deep also. This code is also needed:

change:

$selected_options[$value['target_id']] = $value['target_id'];

to:

if(is_array($value['target_id']) && array_key_exists('target_id', $value['target_id'])){
  $selected_options[$value['target_id']['target_id']] = $value['target_id'];
}
else{
  $selected_options[$value['target_id']] = $value['target_id'];
}
ntsekov’s picture

Proteo’s picture

Hi, I'm having the very same problem but with a different source (a taxonomy field) and the Autosave module, wich also makes an AJAX request when restoring previously saved values. Not sure If I should open a new issue, but since this is basically the same problem I think we could handle it here.

The problem is that the value is sent not as a string but as a nested array, for example:

Array
(
  [tid] => Array
    (
      [tid] => 37
    )
)

I think a way to address the issue is to make sure that the value passed to the $selected_options array has been flattened in a more generic way, such as:

elseif (isset($value['tid'])) { // With CCK, it's an array. Taxonomy
  if (is_array($value['tid'])) {
    // Get the value flattening the array.
    $arr = array_column($value, 'tid');
    $tid = reset($arr);
  }
  else {
    $tid = $value['tid'];
  }
  $selected_options[$tid] = $tid;
}

Patch against the 7.x-1.x branch is provided.