If you have a theme that defines its own menu class like:

/**
 * Returns HTML for a wrapper for a menu sub-tree.
 */
function mytheme_menu_tree($variables) {
  return '<ul class="foo-menu">' . $variables['tree'] . '</ul>';
}

the book menu theming becomes a real mess as you need to add a function for every single book menu, but you do not know when a user creates a new book id.

function mytheme_menu_tree__book_toc_4($variables) {
  return '<ul class="menu">' . $variables['tree'] . '</ul>';
}
function mytheme_menu_tree__book_toc_5($variables) {
  return '<ul class="menu">' . $variables['tree'] . '</ul>';
}

This shows once more how inflexible menu theming is.

I think D7 book module should add this:

/**
 * Implements hook_theme().
 */
function book_theme($existing, $type, $theme, $path) {
  // Book module assigns an incrementing bid to every new book and
  // menu_tree_output() does not add any useful wildcard suggestion.
  $books = db_query('SELECT DISTINCT bid FROM {book}');
  foreach ($books as $book) {
    // Override theming for every single book id.
    $items['menu_tree__book_toc_' . $book->bid] = array(
      'render element' => 'tree',
      'function' => 'theme_book_tree',
      'preprocess functions' => array('template_preprocess_book_tree'),
    );
  }

  return $items;
}

/**
 * Returns HTML for a wrapper for a book menu sub-tree.
 *
 * @param $variables
 *   An associative array containing:
 *   - tree: An HTML string containing the tree's items.
 *
 * @see template_preprocess_book_menu_tree()
 * @ingroup themeable
 */
function theme_book_tree($variables) {
  return '<ul class="menu">' . $variables['tree'] . '</ul>';
}

/**
 * Implements template_preprocess_HOOK() for theme_book_menu_tree().
 */
function template_preprocess_book_tree(&$variables) {
  $variables['tree'] = $variables['tree']['#children'];
}

Comments

hass’s picture

Issue summary: View changes
hass’s picture

Navbar module has already added the same workaround.

hass’s picture

hass’s picture

StatusFileSize
new2.05 KB

Replaced one navbar note.

hass’s picture

Issue summary: View changes
hass’s picture

Issue summary: View changes
hass’s picture

Version: 8.0.x-dev » 7.x-dev

Looks like this has already been fixed in D8 long time ago.

hass’s picture

Status: Active » Needs review
StatusFileSize
new2.03 KB

Renamed functions to D8 names

hass’s picture

Issue summary: View changes
hass’s picture

StatusFileSize
new2.02 KB

Typos fixed

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.