0

I have a book with some child pages and I'd like to show the last modified date of each corresponding node near them in their menu tree links, or a red badge near the nodes updated during eg. the last 3 days... How to pass the changed_date of nodes to menu links?

Thank you

2
  • are you using the book module?
    – Ziftman
    Dec 3 '19 at 11:05
  • Yes, the core book module
    – Cheope
    Dec 4 '19 at 8:40
0

Hook insert/update

The Book module update the book menu title during node insert/update.

You can implement hook_node_insert and hook_node_update to update the title in place.

/**
 * Implements hook_node_insert().
 */
function my_custom_module_node_insert($node) {
  if (!empty($node->book['bid'])) {
    $node->title = $node->title . format_date($node->changed, 'custom', 'M j, Y');
    _book_update_outline($node);
  }
}

/**
 * Implements hook_node_update().
 */
function my_custom_module_node_update($node) {
  if (!empty($node->book['bid'])) {
    $node->title = $node->title . format_date($node->changed, 'custom', 'M j, Y');
    _book_update_outline($node);
  }
}

Using preprocess

You can do that by override template_preprocess_book_navigation which contain a call for book_children function. The aim of override is to provide custom version of book_children function which add the node date to the title before calling the render method.

/**
 * Processes variables for book-navigation.tpl.php.
 * 
 * Override theme_preprocess_book_navigation
 */
function my_custom_module_preprocess_book_navigation(&$variables) {
  $book_link = $variables['book_link'];
  if ($book_link['mlid']) {
    $variables['tree'] = custom_book_children($book_link);
  }
}

/**
 * Formats the menu links for the child pages of the current page.
 * 
 * A custom version of book_children to add date suffix to link title.
 */
function custom_book_children($book_link) {
  $flat = book_get_flat_menu($book_link);

  $children = array();

  if ($book_link['has_children']) {
    // Walk through the array until we find the current page.
    do {
      $link = array_shift($flat);
    }
    while ($link && ($link['mlid'] != $book_link['mlid']));
    // Continue though the array and collect the links whose parent is this page.
    while (($link = array_shift($flat)) && $link['plid'] == $book_link['mlid']) {
      // Get node object.
      $node = menu_get_object('node', 1, $link['link_path']);
      // Add date to link title.
      $link['title'] = $link['title'] . ' - ' . format_date($node->changed, 'custom', 'M j, Y');
      $data['link'] = $link;
      $data['below'] = '';
      $children[] = $data;
    }
  }

  if ($children) {
    $elements = menu_tree_output($children);
    return drupal_render($elements);
  }
  return '';
}
1
  • Thank you very much. I was still struggling on theme_menu_link() trying to pass the $node->changed with no luck. Anyway, the preprocess method works.I added $link['localized_options']['attributes']['class'][] to add a css class too. Thank you again
    – Cheope
    Dec 9 '19 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.