开发者

How do I get Drupal's book module to jump to the first chapter when clicking the book?

开发者 https://www.devze.com 2023-01-09 01:43 出处:网络
I have a page listing all of the books, e.g. book 1 [description] book 2 [description] book 3 [de开发者_高级运维scription]

I have a page listing all of the books, e.g.

book 1
  [description]
book 2
  [description]
book 3
  [de开发者_高级运维scription]

If you click on book 1, you're taken to a page with the exact same description you just saw, with a table of contents. I would rather the clicking on book 1 took you to the first page.

I'm playing around in the book module, and can't figure out where to get started.


After some hours struggling with the same issue I found a nice solution. (using Drupal 7.2) You need to create a preprocess hook for book_navigation.

Some improvements :

  • Users having outline privilege can access the top-level page (useful to add a 1st level child)
  • The prev link on the first child page, to the top-level page, is removed.

I think even if admin user can access the book top-level page it's more SEO-friendly to make a 301 redirect when trying to reach the top-level page for other users.

Here is the code :


  /**
   * This preprocess hook avoids the top-level page of a book to be displayed.
   * Instead, if the top-level book is being requested, user is redirected to
   * the first child page.
   * This only occurs if user does not have outline permission.
   * Also on the first child page, the prev link to the top-level page is removed.
   */
  function mytheme_alpha_preprocess_book_navigation(&$variables) {
    template_preprocess_book_navigation($variables);

    // normal behaviour for privileged users
    if(user_access('administer book outlines')) return;

    // redirect to first child
    if($variables['current_depth']==1) {
      $first_child_link = book_next($variables['book_link']);
      if($first_child_link['link_path']) {
        drupal_goto($first_child_link['link_path'],array(),301);
      }
    }
    // Remove prev link for first child
    // and remove up link for first level children
    if($variables['current_depth']==2) {
      if($variables['parent_url'] == $variables['prev_url']) $variables['prev_url']='';
      $variables['parent_url']='';
    }
  }

NOTE: I'm using omega theme so the hook prefix is mytheme_alpha.

0

精彩评论

暂无评论...
验证码 换一张
取 消