Commit 63312b29 authored by Dries's avatar Dries

- Patch #825330 by rfay, katbailey: make book module use standard D7 AJAX forms techniques.

parent 16eb932f
......@@ -189,13 +189,6 @@ function book_menu() {
'type' => MENU_CALLBACK,
'file' => 'book.pages.inc',
);
$items['book/js/form'] = array(
'page callback' => 'book_form_update',
'delivery callback' => 'ajax_deliver',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'file' => 'book.pages.inc',
);
return $items;
}
......@@ -481,6 +474,8 @@ function _book_parent_select($book_link) {
'#description' => t('The parent page in the book. The maximum depth for a book and all child pages is !maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
'#options' => book_toc($book_link['bid'], $book_link['parent_depth_limit'], array($book_link['mlid'])),
'#attributes' => array('class' => array('book-title-select')),
'#prefix' => '<div id="edit-book-plid-wrapper">',
'#suffix' => '</div>',
);
}
......@@ -491,8 +486,11 @@ function _book_parent_select($book_link) {
* Build the common elements of the book form for the node and outline forms.
*/
function _book_add_form_elements(&$form, &$form_state, $node) {
// Need this for AJAX.
$form_state['cache'] = TRUE;
// If the form is being processed during the AJAX callback of our book bid
// dropdown, then $form_state will hold the value that was selected.
if (isset($form_state['values']['book'])) {
$node->book = $form_state['values']['book'];
}
$form['book'] = array(
'#type' => 'fieldset',
......@@ -557,7 +555,7 @@ function _book_add_form_elements(&$form, &$form_state, $node) {
'#weight' => -5,
'#attributes' => array('class' => array('book-title-select')),
'#ajax' => array(
'path' => 'book/js/form',
'callback' => 'book_form_update',
'wrapper' => 'edit-book-plid-wrapper',
'effect' => 'fade',
'speed' => 'fast',
......@@ -565,6 +563,19 @@ function _book_add_form_elements(&$form, &$form_state, $node) {
);
}
/**
* Renders a new parent page select element when the book selection changes.
*
* This function is called via AJAX when the selected book is changed on a node
* or book outline form.
*
* @return
* The rendered parent page select element.
*/
function book_form_update($form, $form_state) {
return $form['book']['plid'];
}
/**
* Common helper function to handles additions and updates to the book outline.
*
......
......@@ -217,35 +217,3 @@ function book_remove_form_submit($form, &$form_state) {
}
$form_state['redirect'] = 'node/' . $node->nid;
}
/**
* Renders a new parent page select element when the book selection changes.
*
* This function is called via AJAX when the selected book is changed on a node
* or book outline form. It creates a new parent page select element, adds it
* to the cached form, and then returns the rendered element so it can be
* displayed on the form.
*
* @return
* The rendered parent page select element.
*/
function book_form_update() {
// Load the form based upon the $_POST data sent via the ajax call.
list($form, $form_state) = ajax_get_form();
$bid = $_POST['book']['bid'];
// Validate the bid.
if (isset($form['book']['bid']['#options'][$bid])) {
$book_link = $form['#node']->book;
$book_link['bid'] = $bid;
// Get the new options and update the cache.
$form['book']['plid'] = _book_parent_select($book_link);
form_set_cache($form['values']['form_build_id'], $form, $form_state);
// Build the new select element and return it.
$form_state = array();
$form = form_builder($form['form_id']['#value'], $form, $form_state);
return $form['book']['plid'];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment