php - Entity metadata wrapper -
i'm getting error metadata wrapper. have field test => entity reference multiple selection list.i following error entitymetadatawrapperexception : invalid data value given. sure matches required data type , format.
$account = entity_load_single('user', $user->uid); $acc_wrapper = entity_metadata_wrapper('user', $account); $list = $acc_wrapper->test->value(); $exists = false; if (!empty($list)) { foreach ($list $item) { if ($item->nid == $form_state['storage']['node']->nid) { $exists = true; break; } } } if (!$exists) { if (!$list) { $list = array(); $list[] = $form_state['storage']['node']->nid; } $acc_wrapper->test->set($list); $acc_wrapper->save();
1rst quick tips
$account = entity_load_single('user', $user->uid); $acc_wrapper = entity_metadata_wrapper('user', $account);
you don't need load entity unless need loaded after (or it's loaded). need id, , let entity_metadata_wrapper magic operate.
$acc_wrapper = entity_metadata_wrapper('user', $user->uid);
i think error here
if (!$list) { $list = array(); $list[] = $form_state['storage']['node']->nid; }
$list initiated because of "$list = $acc_wrapper->test->value();", never fullfill condition, , trying set , save (because missing '}' )... makes no sense...
could try version ?
$acc_wrapper = entity_metadata_wrapper('user', $user->uid); $list = $acc_wrapper->test->value(); $exists = false; if (!empty($list)) { foreach ($list $item) { if ($item->nid == $form_state['storage']['node']->nid) { $exists = true; break; } } } if (!$exists && !$list) { $list = array($form_state['storage']['node']->nid); $acc_wrapper->test = $list; $acc_wrapper->save(); }
Comments
Post a Comment