#StackBounty: #7 #forms #ctools Update entity ref field in parent form with new node created in ctools modal

Bounty: 50

I have a node_add form for Foo node-type with an entity reference field to Bar node-type. I’m trying to allow users to create Bar nodes inline from the Foo create form.

So far I’ve been able to get the ctools modal to popup, create and process the new Bar node and dpm the new Bar-type nid. but I’m stuck on how to push that to the parent Foo form and add this new Bar node into the entity reference field.

Here’s what I tried using ajax_command_append in my callback for the ctools modal, but it doesn’t append the new Bar node to the entity reference field values in the parent form:

function mymodule_add_bar_callback($js = FALSE) {

ctools_include('node.pages', 'node', '');

$node_type = 'Bar'; 
$node = (object)array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => $node_type,
    'language' => LANGUAGE_NONE,

$form_state = array(
    'title' => t('ADD A NEW BAR'),
    'ajax' => TRUE,

$form_state['build_info']['args'] = array($node);
$commands = ctools_modal_form_wrapper($node_type . '_node_form', $form_state);

// if the form was submitted, reload parent Event-Add form
if (!empty($form_state['executed'])) {
    $commands = array();
    $commands[] = ctools_modal_command_dismiss();
    $commands[] = ajax_command_append('#edit-field-bar-und-0-target-id', '<input type="text" id="edit-field-bar-und-1-target-id" name="field_bar[und][1][target_id]" value="" size="100" maxlength="1024" value="' .  $form_state['node']->title . ' (' .  $form_state['node']->nid . ')" class="form-text required form-autocomplete" autocomplete="OFF" aria-autocomplete="list">');

    $output = $commands;

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.