function book_helper_admin_edit_submit in Book helper 7
Same name and namespace in other branches
- 6 book_helper.admin.inc \book_helper_admin_edit_submit()
Handle submission of the book administrative page form.
This function takes care to save parent menu items before their children. Saving menu items in the incorrect order can break the menu tree.
See also
File
- ./
book_helper.admin.inc, line 176 - Administration page for the 'Book helper' module.
Code
function book_helper_admin_edit_submit($form, &$form_state) {
$node = $form['#node'];
// Custom code: Allow top-level pages to be removed from books. http://drupal.org/node/283045
if ($form_state['values']['op'] == t('Revert')) {
menu_link_delete($node->book['mlid']);
db_delete('book')
->condition('bid', $node->nid)
->execute();
db_delete('menu_links')
->condition('menu_name', 'book-toc-' . $node->nid)
->execute();
drupal_set_message(t('The post has been reverted from the book.'));
$form_state['redirect'] = 'node/' . $node->nid;
return;
}
// Save elements in the same order as defined in post rather than the form.
// This ensures parents are updated before their children, preventing orphans.
$order = array_flip(array_keys($form_state['input']['table']));
$form['table'] = array_merge($order, $form['table']);
foreach (element_children($form['table']) as $key) {
if ($form['table'][$key]['#item']) {
$row = $form['table'][$key];
$values['book'] = $form_state['values']['table'][$key];
$node = node_load($values['book']['nid']);
// Fill the menu link values into the node.
$link_changed = _book_helper_fill_link($node, $values);
// Check if node title has changed.
$old_title = $row['title']['#default_value'];
$new_title = $values['book']['title'];
// Do a full node save only if a change of title and revisions are enabled.
if ($new_title != $old_title && !variable_get('book_helper_order_disable_revisions', 1)) {
$node->title = $new_title;
$node->revision = 1;
$node->log = t('Title changed from %original to %current.', array(
'%original' => $old_title,
'%current' => $new_title,
));
// This updates everything via hooks.
node_save($node);
watchdog('content', 'book: updated %title.', array(
'%title' => $new_title,
), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
}
else {
global $user;
// Just save the elements according to what's changed.
if ($new_title != $old_title) {
$node->title = $new_title;
// Save the title field directly.
drupal_write_record('node', $node, 'nid');
_node_save_revision($node, $user->uid, 'vid');
}
if ($link_changed) {
_book_helper_outline_submit($node);
}
}
}
}
drupal_set_message(t('Updated book %title.', array(
'%title' => $form['#node']->title,
)));
}