function panels_common_ajax_context_item_add in Panels 5.2
Ajax entry point to add an context
1 call to panels_common_ajax_context_item_add()
- panels_common_ajax in includes/
common.inc - Incoming menu function for ajax calls. This routes to the proper 'module' -- we really only need this because common.inc relies on panels.module for its menu hook, and this way the code stays together.
File
- includes/
common.inc, line 870 - Functions used by more than one panels client module.
Code
function panels_common_ajax_context_item_add($module, $type, $panel_name) {
$object = panels_common_cache_get("panel_object:{$module}", $panel_name);
if (!$object || !$type) {
panels_ajax_render();
}
// Figure out which context we're adding
if (isset($_POST['buttons'][$type]['item'])) {
$name = $_POST['buttons'][$type]['item'];
// Unset $_POST so fapi doesn't get confused and try to process this
// as a form.
unset($_POST);
}
else {
if (isset($_POST[$type]['name'])) {
$name = $_POST[$type]['name'];
}
}
if (empty($name)) {
panels_ajax_render();
}
$info = panels_common_context_data($type, $name);
if (empty($info)) {
panels_ajax_render();
}
// Create a reference to the place our context lives.
$keyword = $type . 's';
$ref =& $object->{$keyword};
// Give this argument an id, which is really just the nth version
// of this particular context.
$id = panels_common_get_arg_id($ref, $name) + 1;
// Figure out the position for our new context.
$position = empty($ref) ? 0 : max(array_keys($ref)) + 1;
// Create the basis for our new context.
$ref[$position] = array(
'identifier' => $info['title'] . ($id > 1 ? ' ' . $id : ''),
'keyword' => panels_common_get_keyword($object, $info['keyword']),
'id' => $id,
);
$contexts = panels_context_load_contexts($object);
$form_id = 'panels_common_edit_' . $type . '_form';
$form = drupal_retrieve_form($form_id, $object, $info, $position, $contexts);
if ($_POST && $_POST['form_id'] == $form_id) {
$form['#redirect'] = FALSE;
}
$retval = drupal_process_form($form_id, $form);
if ($retval) {
// successful submit
// Save changes
$ref[$position] = $retval;
panels_common_cache_set("panel_object:{$module}", $panel_name, $object);
// Build a chunk of the form to merge into the displayed form
$arg_form[$type] = array(
'#tree' => TRUE,
);
panels_common_add_item_to_form($type, $arg_form[$type], $position, $retval);
$arg_form = form_builder($form_id, $arg_form);
// Build the relationships table so we can ajax it in.
// This is an additional thing that goes in here.
$rel_form = array(
'#theme' => 'panels_common_context_item_form',
'#panel_name' => $panel_name,
'#panels_context_type' => 'relationship',
'#panels_context_module' => $module,
'#only_buttons' => TRUE,
);
$rel_form['relationship'] = array(
'#tree' => TRUE,
);
$available_relationships = panels_get_relevant_relationships(panels_context_load_contexts($object));
$output = new stdClass();
if (!empty($available_relationships)) {
panels_common_add_item_table_buttons('relationship', $rel_form, $available_relationships);
$rel_form = form_builder('dummy_form_id', $rel_form);
$output->relationships_table = drupal_render($rel_form);
}
$output->type = 'add';
$output->output = theme('panels_common_context_item_row', $type, $arg_form[$type], $position, $position);
$output->position = $position;
panels_ajax_render($output);
}
else {
$type_info = panels_common_context_info($type);
$title = t('Add @type "@context"', array(
'@type' => $type_info['singular title'],
'@context' => $info['title'],
));
$output = theme('status_messages');
$output .= drupal_render_form($form_id, $form);
panels_ajax_render($output, $title, url($_GET['q'], NULL, NULL, TRUE));
}
}