1

I have a multi-value field type in a content type. During hook_form_alter(), I add an #ajax property to the field.

Upon execution, the ajax callback loads a node's custom view mode and two form inputs from another content type into the div.

I am using field_default_form() to generate the form elements.

In order for these addons to persist through clicking the 'Add more' button, I use hook_form_alter() to call the same function that is called by the ajax callback, and I add them into the results/wrapper div.

My problem is that the form elements of the #ajax callback results are not rendered correctly. For example, they do not have a name or id for the <input> tag. One of the fields is an autocomplete field and autocomplete functionality does not get applied to the element.

However, the results of the 'add more' button, which calls hook_form_alter(), DOES render these elements correctly.

As far as I can tell, the render arrays are identical during generation. It seems to me like some additional processing happens after hook_form_alter() that does not happen after my #ajax callback.

I have tried adding #theme, #theme_wrapper, #after_build and #rebuild = TRUE keys to the resulting render array elements, but it does not appear to have any effect.

How can I get these elements to render correctly?

1 Answer 1

1

Since hook_form_alter() is returning the correct results, there is a way to use that to our advantage.

By calling drupal_rebuild_form() in our ajax callback, we can return the specific part of the resulting array that should fill in our wrapper div.

e.g.

function _send_item(&$form, &$form_state) {

    $delta   = $form_state['triggering_element']['#delta'];
    $lang    = $form_state['triggering_element']['#language'];

    $new_form = drupal_rebuild_form('mymodule_node_form', $form_state);

    return $new_form['mymodule_item_id'][$lang][$delta]['item_data'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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