diff --git a/core/modules/book/book.admin.inc b/core/modules/book/book.admin.inc index 014b899..ff5dac8 100644 --- a/core/modules/book/book.admin.inc +++ b/core/modules/book/book.admin.inc @@ -101,17 +101,17 @@ function book_admin_edit_submit($form, &$form_state) { $values = $form_state['values']['table'][$key]; // Update menu item if moved. - if ($row['parent']['plid']['#default_value'] != $values['parent']['plid'] || $row['weight']['#default_value'] != $values['weight']) { - $menu_link = entity_load('menu_link', $values['parent']['mlid']); + 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['parent']['plid']; + $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($row['#nid']); + $node = node_load($values['nid']); $langcode = LANGUAGE_NOT_SPECIFIED; $node->title = $values['title']; $node->book['link_title'] = $values['title']; @@ -145,14 +145,8 @@ function book_admin_edit_submit($form, &$form_state) { */ function _book_admin_table(EntityInterface $node, &$form) { $form['table'] = array( - '#type' => 'table', + '#theme' => 'book_admin_table', '#tree' => TRUE, - '#header' => array(t('Title'), t('Weight'), t('Parent'), t('Operations')), - '#empty' => t('No book content available.'), - '#tabledrag' => array( - array('order', 'sibling', 'book-weight'), - array('match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2), - ), ); $tree = book_menu_subtree_data($node->book); @@ -171,6 +165,7 @@ function _book_admin_table(EntityInterface $node, &$form) { ); _book_admin_table_tree($tree['below'], $form['table']); } + } /** @@ -191,76 +186,107 @@ function _book_admin_table_tree($tree, &$form) { $count = count($tree); $delta = ($count < 30) ? 15 : intval($count / 2) + 1; - $access = user_access('administer nodes'); - $destination = drupal_get_destination(); - foreach ($tree as $data) { - $id = 'book-admin-' . $data['link']['nid']; - - $form[$id]['#item'] = $data['link']; - $form[$id]['#nid'] = $data['link']['nid']; - $form[$id]['#attributes']['class'][] = 'draggable'; - $form[$id]['#weight'] = $data['link']['weight']; - - $form[$id]['title'] = array( - '#prefix' => theme('indentation', array('size' => $data['link']['depth'] - 2)), - '#type' => 'textfield', - '#default_value' => $data['link']['link_title'], - '#maxlength' => 255, - '#size' => 40, - ); - - $form[$id]['weight'] = array( - '#type' => 'weight', - '#default_value' => $data['link']['weight'], - '#delta' => max($delta, abs($data['link']['weight'])), - '#title' => t('Weight for @title', array('@title' => $data['link']['title'])), - '#title_display' => 'invisible', - '#attributes' => array( - 'class' => array('book-weight'), + $form['book-admin-' . $data['link']['nid']] = array( + '#item' => $data['link'], + 'nid' => array('#type' => 'value', '#value' => $data['link']['nid']), + 'depth' => array('#type' => 'value', '#value' => $data['link']['depth']), + 'href' => array('#type' => 'value', '#value' => $data['link']['href']), + 'title' => array( + '#type' => 'textfield', + '#default_value' => $data['link']['link_title'], + '#maxlength' => 255, + '#size' => 40, ), - ); - - $form[$id]['parent']['plid'] = array( - '#type' => 'hidden', - '#default_value' => $data['link']['plid'], - '#attributes' => array( - 'class' => array('book-plid'), + 'weight' => array( + '#type' => 'weight', + '#default_value' => $data['link']['weight'], + '#delta' => max($delta, abs($data['link']['weight'])), + '#title' => t('Weight for @title', array('@title' => $data['link']['title'])), + '#title_display' => 'invisible', ), - ); - $form[$id]['parent']['mlid'] = array( - '#type' => 'hidden', - '#default_value' => $data['link']['mlid'], - '#attributes' => array( - 'class' => array('book-mlid'), + 'plid' => array( + '#type' => 'hidden', + '#default_value' => $data['link']['plid'], + ), + 'mlid' => array( + '#type' => 'hidden', + '#default_value' => $data['link']['mlid'], ), ); + if ($data['below']) { + _book_admin_table_tree($data['below'], $form); + } + } + + return $form; +} + +/** + * Returns HTML for a book administration form. + * + * @param array $variables + * An associative array containing: + * - form: A render element representing the form. + * + * @see book_admin_table() + * @ingroup themeable + */ +function theme_book_admin_table($variables) { + $form = $variables['form']; + + drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2); + drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight'); + + $header = array(t('Title'), t('Weight'), t('Parent'), t('Operations')); + + $rows = array(); + $destination = drupal_get_destination(); + $access = user_access('administer nodes'); + foreach (element_children($form) as $key) { + $nid = $form[$key]['nid']['#value']; + $href = $form[$key]['href']['#value']; + + // Add special classes to be used with tabledrag.js. + $form[$key]['plid']['#attributes']['class'] = array('book-plid'); + $form[$key]['mlid']['#attributes']['class'] = array('book-mlid'); + $form[$key]['weight']['#attributes']['class'] = array('book-weight'); - $form[$id]['operations'] = array( - '#type' => 'operations', - '#links' => array(), + $data = array( + theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']), + drupal_render($form[$key]['weight']), + drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']), ); - $form[$id]['operations']['#links']['view'] = array( + $links = array(); + $links['view'] = array( 'title' => t('View'), - 'href' => $data['link']['href'], + 'href' => $href, ); if ($access) { - $form[$id]['operations']['#links']['edit'] = array( + $links['edit'] = array( 'title' => t('Edit'), - 'href' => "node/$id/edit", + 'href' => "node/$nid/edit", 'query' => $destination, ); - $form[$id]['operations']['#links']['delete'] = array( + $links['delete'] = array( 'title' => t('Delete'), - 'href' => "node/$id/delete", + 'href' => "node/$nid/delete", 'query' => $destination, ); } - - if ($data['below']) { - _book_admin_table_tree($data['below'], $form); + $data[] = array( + 'data' => array( + '#type' => 'operations', + '#links' => $links, + ), + ); + $row = array('data' => $data); + if (isset($form[$key]['#attributes'])) { + $row = array_merge($row, $form[$key]['#attributes']); } + $row['class'][] = 'draggable'; + $rows[] = $row; } - return $form; + return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'book-outline'), 'empty' => t('No book content available.'))); } diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 09fdc3d..b6b6ab7 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -51,6 +51,9 @@ function book_theme() { 'variables' => array('title' => NULL, 'contents' => NULL, 'depth' => NULL), 'template' => 'book-export-html', ), + 'book_admin_table' => array( + 'render element' => 'form', + ), 'book_all_books_block' => array( 'render element' => 'book_menus', 'template' => 'book-all-books-block',