function book_admin_edit_submit

Form submission handler for book_admin_edit().

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

book_admin_edit_validate()

menu_overview_form_submit()

File

drupal/core/modules/book/book.admin.inc, line 57
Administration page callbacks for the Book module.

Code

function book_admin_edit_submit($form, &$form_state) {

  // 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']);

  // Track updates.
  $updated = FALSE;
  foreach (element_children($form['table']) as $key) {
    if ($form['table'][$key]['#item']) {
      $row = $form['table'][$key];
      $values = $form_state['values']['table'][$key];

      // Update menu item if moved.
      if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
        $menu_link = entity_load('menu_link', $values['mlid']);
        $menu_link->weight = $values['weight'];
        $menu_link->plid = $values['plid'];
        $menu_link
          ->save();
        $updated = TRUE;
      }

      // Update the title if changed.
      if ($row['title']['#default_value'] != $values['title']) {
        $node = node_load($values['nid']);
        $langcode = Language::LANGCODE_NOT_SPECIFIED;
        $node->title = $values['title'];
        $node->book['link_title'] = $values['title'];
        $node
          ->setNewRevision();
        $node->log = t('Title changed from %original to %current.', array(
          '%original' => $node->title,
          '%current' => $values['title'],
        ));
        $node
          ->save();
        watchdog('content', 'book: updated %title.', array(
          '%title' => $node
            ->label(),
        ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
      }
    }
  }
  if ($updated) {

    // Flush static and cache.
    drupal_static_reset('book_menu_subtree_data');
    $cid = 'links:' . $form['#node']->book['menu_name'] . ':subtree-cid:' . $form['#node']->book['mlid'];
    cache('menu')
      ->delete($cid);
  }
  drupal_set_message(t('Updated book %title.', array(
    '%title' => $form['#node']
      ->label(),
  )));
}