function noderelationships_noderef_ahah_search in Node Relationships 6
Menu callback for AHAH management of multiple value fields.
See also
1 string reference to 'noderelationships_noderef_ahah_search'
- _noderelationships_menu in ./
noderelationships.system.inc - Implementation of hook_menu().
File
- ./
noderelationships.pages.inc, line 794 - Implementation of user land pages.
Code
function noderelationships_noderef_ahah_search($type_name, $field_name, $item_count) {
// Include node form code from CCK.
module_load_include('inc', 'content', 'includes/content.node_form');
$type = content_types($type_name);
$field = content_fields($field_name, $type['type']);
if ($field['multiple'] != 1 || empty($_POST['form_build_id'])) {
// Invalid request.
drupal_json(array(
'data' => '',
));
exit;
}
// Retrieve the cached form.
$form_state = array(
'submitted' => FALSE,
);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
if (!$form) {
// Invalid form_build_id.
drupal_json(array(
'data' => '',
));
exit;
}
// Define the requested number of "empty" multiple values.
if (!is_numeric($item_count) || $item_count <= 0) {
$item_count = 1;
}
$_POST[$field_name] = array();
for ($i = 0; $i < $item_count; $i++) {
$_POST[$field_name][] = array(
'_weight' => $i,
);
}
// We don't simply return a new empty widget to append to existing ones, because
// - ahah.js won't simply let us add a new row to a table
// - attaching the 'draggable' behavior won't be easy
// So we resort to rebuilding the whole table of widgets including the existing ones,
// which makes us jump through a few hoops.
// The form that we get from the cache is unbuilt. We need to build it so that
// _value callbacks can be executed and $form_state['values'] populated.
// We only want to affect $form_state['values'], not the $form itself
// (built forms aren't supposed to enter the cache) nor the rest of $form_data,
// so we use copies of $form and $form_data.
$form_copy = $form;
$form_state_copy = $form_state;
$form_copy['#post'] = array();
form_builder($_POST['form_id'], $form_copy, $form_state_copy);
// Reset cached ids, so that they don't affect the actual form we output.
form_clean_id(NULL, TRUE);
// Sort the $form_state['values'] we just built *and* the incoming $_POST data
// according to d-n-d reordering.
unset($form_state['values'][$field_name][$field['field_name'] . '_add_more']);
$form_state['values'][$field_name] = array();
foreach ($_POST[$field_name] as $delta => $item) {
$form_state['values'][$field_name][$delta]['_weight'] = $item['_weight'];
$form_state['values'][$field_name][$delta]['_remove'] = isset($item['_remove']) ? $item['_remove'] : 0;
}
$form_state['values'][$field_name] = _content_sort_items($field, $form_state['values'][$field_name]);
$_POST[$field_name] = _content_sort_items($field, $_POST[$field_name]);
// Build our new form element for the whole field, asking for one more element.
$form_state['item_count'] = array(
$field_name => count($_POST[$field_name]),
);
$form_element = content_field_form($form, $form_state, $field);
// Let other modules alter it.
// We pass an empty array as hook_form_alter's usual 'form_state' parameter,
// instead of $form_atate (for reasons we may never remember).
// However, this argument is still expected to be passed by-reference
// (and PHP5.3 will throw an error if it isn't.) This leads to:
$data =& $form_element;
$empty_form_state = array();
$data['__drupal_alter_by_ref'] = array(
&$empty_form_state,
);
drupal_alter('form', $data, 'content_add_more_js');
// Add the new element at the right place in the (original, unbuilt) form.
if (function_exists('content_set_nested_elements')) {
content_set_nested_elements($form, $field_name, $form_element[$field_name]);
}
elseif (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type['type'], $field_name))) {
$form[$group_name][$field_name] = $form_element[$field_name];
}
else {
$form[$field_name] = $form_element[$field_name];
}
// Save the new definition of the form.
$form_state['values'] = array();
form_set_cache($form_build_id, $form, $form_state);
// Build the new form against the incoming $_POST values so that we can
// render the new element.
$form_state = array(
'submitted' => FALSE,
);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
$form = form_builder($_POST['form_id'], $form, $form_state);
// Render the new output.
if (function_exists('content_get_nested_elements')) {
$field_form = array_shift(content_get_nested_elements($form, $field_name));
}
else {
$field_form = !empty($group_name) ? $form[$group_name][$field_name] : $form[$field_name];
}
// We add a div around the new content to receive the ahah effect.
$field_form['#prefix'] = '<div class="ahah-new-content">' . (isset($field_form['#prefix']) ? $field_form['#prefix'] : '');
$field_form['#suffix'] = (isset($field_form['#suffix']) ? $field_form['#suffix'] : '') . '</div>';
// If a newly inserted widget contains AHAH behaviors, they normally won't
// work because AHAH doesn't know about those - it just attaches to the exact
// form elements that were initially specified in the Drupal.settings object.
// The new ones didn't exist then, so we need to update Drupal.settings
// by ourselves in order to let AHAH know about those new form elements.
$javascript = drupal_add_js(NULL, NULL);
$output_js = isset($javascript['setting']) ? '<script type="text/javascript">jQuery.extend(Drupal.settings, ' . drupal_to_js(call_user_func_array('array_merge_recursive', $javascript['setting'])) . ');</script>' : '';
$output = theme('status_messages') . drupal_render($field_form) . $output_js;
// Using drupal_json() breaks filefield's file upload, because the jQuery
// Form plugin handles file uploads in a way that is not compatible with
// 'text/javascript' response type.
$GLOBALS['devel_shutdown'] = FALSE;
print drupal_to_js(array(
'status' => TRUE,
'data' => $output,
));
exit;
}