Creating a Chapter Jump Menu in Views for Drupal Books

One of the things I enjoy about the Drupal Book module is that it is a built in way to generate hierarchical content for a site. This can be anything from a work of fiction with multiple chapters to a handbook how-to. The built in navigation features are extremely simple to use for the visiting user and frankly make it easier to get them where they need to be. However there are some ways that are easier then others to navigate. One is to have a full listing of the chapters in a sidebar of the page. Personally I don’t quite like that approach in some cases.

On pretty much every fanfiction site I go to there is always a drop down menu to allow the user to go to the chapter of their choice. This allows the user to quickly select what chapter they want to go to without having to back track to a chapter listing. Now, whether this is more efficient then just having your table of contents along the side of the screen or not I could tell you. However it is expected in this day and age that it be there. So I had to figure out how to do this with Drupal. Fortunately after a bit I figured it out.

First off you more then likely already have views installed. It’s such a necessary module it’s been included as a core module in Drupal 8. IT allows you to create pages and listing of content easily and handles many of the use cases your average website will need. It also integrates rather seamlessly into Panels, allowing you to add views pages or create view panes. So first off you want to create a brand new view. It’s going to be a content view of the content type you use as pages in a book. I personally have two separate content types as part of my books. One type for the meta-data: title, summary, genre, series, rating. The second content type is the chapter or the real content of the book. I call the second type chapter so that is how I will refer to things from here on. You don’t need a full page so don’t create a page, you can choose a block or leave the basic two empty then once in editing create a content pane.

Now there is a module you will need to install called menu node API. This module provides you with an API, a module for setting access permissions, and the important part for this a node integrating it all with views. We will need this as for all intents and purposes a book module is just a menu of content and that menu is important for this block. Also, you should have pathauto installed so you can set your custom URL paths for your child page content type.

Having these modules installed first go to admin/config/search/path/patterns and find the entry for our chapter page. Now pathauto uses tokens for a good portion of things. My pathauto entries for my Story page, the front page for all my books is stories/[node:book:mlid] so that the mlid of this page is the same even when looking at the parent in your URL. My chapter page path is stories/[node:book:root:mlid]/[node:nid]. This provides some URL consistency so that the parent id is always the same no matter where in the book you are. Where I put [node:nid] you can chose what you want there instead. However, the [node:book:root:mlid] is essential to what we are discussing. Delete and Bulk Update your content URLs using the new patterns.

Now you have your basic content pane and your URL alias default. The first thing you need is to add a contextual filter. You should find the Menu: Parent Menu Link ID entry in the contextual filters and select that. When in settings, and this is how I did it if you know a better way then please share, under “When filter is not available” select provide default value and set that to Raw Value from URL. You will then need to set the path component to read for this filter. That would be where in your alias the menu link id of the book is located. In my URL, stories/[node:book:root:mlid]/[node:nid], that would put the value you want as the second part of the path. Always remember that it’s position in the URL is relative to the end of your domain address, so which value in between / it is. For us that sets the value at 2. Then click the check box for use alias as the system path will NOT have this in there. Under the entries for when a default value r the filter is available I set it for a basic validation and to hide the view if it doesn’t.

Now go to your format and set it for jump menu. And then make sure your show entry is set for fields. You will need four fields here. The first field is the title of the chapter. That is going to be your drop menus human readable entry. The second field is the parent menu link id. Set it to be excluded from display as you do NOT need it visible to the user. You will also need to add the node nid. This also should be set to exclude from display. Your forth field is the actual link that will be constructed. Add the Content:Link field, and once again click the exclude from display box. Go to Rewrite Results and select rewrite output of this field. Your entry should be similar to stories/[plid]/[nid] where the [plid] and [nid] fields are the values provided by the second and third fields.

Now go to your settings for the Jump Menu format. You need to set the link field as being Content:Link so that it will consider that as the internal link to send your user to. I personally hide the go button and set my choose text to -Chapter- however you can leave Go visible and modify the text of it to your preference.

Go to your preview area and enter the parent menu link id of one of your books to test. What you should see is a basic drop down menu of all chapters that are a part of that book. Save this and then just place the view pane in a panel page modifying your chapter content type. There you go, and easy to use chapter jump menu for the users on your site. There may be a more efficient and easier way to do this. This is just what worked for me. If you have better ideas or improvements please, leave a comment and let me know what you think would be better. I’m always open to more improvement.