0

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', '');
ctools_include('modal');
ctools_include('ajax');

$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;
}
0

There are few existing modules which gives the same functionality. - https://www.drupal.org/project/entityconnect - https://www.drupal.org/project/references

If you want to handle it in custom way, fetch the field and set the default (target_id) value in the form array element and use drupal_render/drupal_render_children to convert the form element array into html and try to replace it in the front end.

  • Thanks, but I'm locked into ctools and so I need an ajax/ctools solution. – longboardnode Mar 16 '18 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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