Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a custom widget which can be used for a group audience field. Instead of being a dropdown like the OG_AUDIENCE_WIDGET it is made up of three multiple select list, and the group which is actually selected is the one which is picked in the third select list. I have the actual widget working but I am stuck on validation. I have been trying to set the gid in a couple of different ways which have failed in a couple different manors. First:

function multiselect_validate($element, &$form_state)
{
  if($form_state['submitted'])
  {
    $index = array_shift(array_values($element['wrapper']['dropdown_assignment']['#value']));
    $assignment_name = $element['wrapper']['dropdown_assignment']['#options'][$index];
    $query = new EntityFieldQuery();
    $result = $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'assignment_group')
    ->propertyCondition('title',$assignment_name)    
    ->execute();
    $node  = reset($result['node']);
    $assignment_gid = og_get_group("node", $node->nid)->gid;
    $value = $value[LANGUAGE_NONE][0]['gid'] = $assignment_gid;
    form_set_value($element,array('gid'=>198), $form_state);
  }
}

This kind of validation returns an error:

[11-Dec-2012 11:23:07] PHP Fatal error:  Cannot unset string offsets in /Applications/MAMP/htdocs/arcade/modules/field/field.default.inc on line 41

I also tried something like this for the final form_set_value:

form_set_value($element,array('gid'=>$assignment_gid), $form_state);

But when I do this and then look at the result nothing actually gets set into the field.

What is the proper way to set the gid for the field?

share|improve this question
    
I wonder if there is a problem or something that is unclear about my question that is preventing people from answering it. Does anyone have an advice about how I could reform my question to make it better? –  Mike2012 Dec 17 '12 at 17:58
add comment

1 Answer

up vote 2 down vote accepted
+50

I'm assuming the validate callback that you have mentioned in your question is the #element_validate callback for the field widget element.

In that case, form_set_value must be set to a list of items because a field is inherently multi-valued. Each item in this list must conform to the schema of the field.

So, in this case:

$items = array();
$delta = 0;
$gid = ...; // From somewhere
$items[$delta] = array(
  'gid' => $gid,
  'state' => OG_STATE_ACTIVE, // optional
  'created' => REQUEST_TIME - 3600 // optional (yesterday)
);
form_set_value($element, $items, $form_state);

To illustrate this better, I have a sandbox project up on drupal.org. It declares a new widget with hierarchical dropdowns just like you have described. You can check the code directly from http://drupalcode.org/sandbox/amarnus/1868742.git/tree/refs/heads/master or clone the project from http://drupal.org/sandbox/amarnus/1868742. Hope this helps!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.