I'm working with the entity reference module. I have two node types (Type A) and (Type B). The basic concept is to let users reference multiple nodes (type a) into another node (type b).
So I created a custom form with a radio field that shows a list of nodes (type b) and attached it to all nodes (type a) via a block. This form is supposed to let users select the current node (type a) and add it as a reference to a node from the list (type b).
Everything is working as expected except the submit function. How can a new reference be created when a users submits the form?
Example of new reference:
entity_id: $typeb->nid
revision: $typeb->revision
target_id: $typea->nid
This is my form code:
function mymodule_form($form, &$form_state, $op, $edit = array()) {
$options = mymodule_list_options();
$form['list'] = array(
'#type' => 'radios',
'#required' => TRUE,
'#entity_type' => 'node',
'#language' => $langcode,
'#options' => $options,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
"#ajax" => array(
"callback" => "mymodule_form_submit_callback",
"wrapper" => "inline-messages", "effect" => "fade" ,
'progress' => array('type' => 'throbber', 'message' => 'Adding to list...'),
)
);
$form["wrapper"] = array("#markup" => "<div id='inline-messages'></div>");
return $form;
}
function mymodule_list_options($node) {
global $user;
$matches = array();
$result = db_select('node')->fields('node', array('title'))->condition('uid', $user->uid, '=')->condition('type', 'type_b', '=')->orderBy('nid', 'DESC')->range(0, 50)->execute(); // This creates a list of nodes type_b from the current user.
foreach ($result as $us) {
$matches[$us->title] = check_plain($us->title);
}
return $matches;
}
/**
* Implements hook_form_submit().
*/
function mymodule_form_submit_callback($form, &$form_state) {
global $user;
$lid = db_select('node')->fields('node', array('nid'))->condition('title', $form_state['values']['list'], '=')->condition('type', 'type_b', '=')->execute()->fetchCol(); //This gives me the selected type_b nid.
$nid = node_load(arg(1));
//Im lost here...
}
^^^ This the output from the block form. Its attached to all nodes type a. The list is composed of nodes type b from the current user.
I couldn't find anything regarding adding references to a entityreference field.
I apologize in advance if you find this question kind of confusing. I tried to explaining it as best as I could.