September 30, 2015

Workbench moderation works by allowing you to keep a new revision of your content as a draft (or unpublished) while an older revision remains as your published version. Out of the box Drupal handles this beautifully, most of the time. However, once you start adding contributed and/or custom modules, things can start to get hairy.

For example, Path module will generate a new alias each time your node is saved (if you define one in your node’s settings). This is great when the latest revision is your published content, but when the latest revision is a draft, you’ll want the keep the old alias until you publish the draft.

We’re not the first to encounter this problem, and Workbench Path Revision comes to the rescue. It works by bypassing the Path settings when saving a node, and storing the data until you need it when you publish your draft.

I did run into an issue when trying to auto-generate an alias based on Pathauto’s pattern (Pathauto is a contrib module). Workbench Path Revision would always disable the “Generate automatic URL alias” option and never generate an alias. There is a patch in the works.

Where things really start to get complicated is when you want to use views. When you want to display content using views, you generally add a filter to only display published content in order to avoid visitors from seeing content that’s not quite ready yet. This works great. Unless your site uses the following setup…

Our scenario

Imagine a site where you use Panels to make a node’s page a little more configurable. You turn on Panels and create a page that displays your node as a panels page. You can now move around  your fields, maybe putting some on the left and others on the right (let your imagination go wild). Your node has an image field, and you turn to Views in order to display it as a slideshow. You add a “Content: Nid” contextual filter (also known as an argument) and configure it to use the “Content ID from URL” to link the view to your particular node. Finally you add this view to your panel page and create a beautiful piece of content with a stunning slideshow. Publish it, and presto! So far so good.

A week later your boss tells you that you need to change the slideshow, but he wants to approve the changes before your site’s visitors are allowed to see them. Since you have Workbench moderation enabled on the site, you simply create a new draft and update the images and save the draft. You look at the draft’s page… but nothing has changed! What’s going on?

Comparing the published version and the draft you don’t see a difference…

So what went wrong?

Your panels and views are oblivious to revisions at this point. They simply take the published revision and display it. So how can you fix it? This is how.

Assumptions

We will assume you site is a default Drupal site with the following additional modules enabled:

  • Chaos tools, Page manager, Views content panes
  • Panels
  • Views, Views UI
  • Workbench moderation

The following drush command will get you set up nice and quick:
drush en page_manager panels views_ui views_content workbench_moderation

Getting started

Let’s create a new view on your Drupal site to illustrate the problem:

  1. Go to /admin/structure/views/add to add a new view
  2. Give it a name, in this case “Simple view”
  3. Select: Show “Content” of type “Article”
  4. Deselect the “Create a page” checkbox
  5. Click on the “Continue & edit” button
  6. Click on the “+ Add” dropdown and select “Content pane”
  7. Add a “Contextual filter” of type “Content: Nid”
  8. Configure it to “Provide default value” of type “Content ID from URL”
  9. Apply (all displays)
  10. Save the view

We’ll now add this view to a panels page that overrides the default Article node view:

  1. Go to /admin/structure/pages/edit/node_view
  2. If your “Node template” page is not enabled, enable it
  3. Now “Add variant”
  4. Make sure “Variant type” is set to “Panel”
  5. Check the “Selection rules” and “Contexts” checkboxes
  6. Click on the “Create variant” button
  7. We are now setting our criteria: select “Node: type” and press the “Add” button
  8. Check the “Article” checkbox
  9. Click on the “Save” button
  10. Click on the “Continue” button
  11. We are now setting our contexts: our node’s context is already set, nothing to do here
  12. Click on the “Continue” button
  13. Choose your layout: we can keep the defaults
  14. Click on the “Continue” button
  15. Our default Panel settings are fine too
  16. Click on the “Continue” button
  17. Now let’s add our view to the panel: use the cog in the “Center” panel to “Add content”
  18. Select “View panes” on the left
  19. Select “View: Simple view” (the view pane we created above)
  20. Click on the “Finish” button
  21. We’ll add our a body field as well: use the cog in the “Center” panel to “Add content”
  22. Select “Node” on the left
  23. Select “Node body”
  24. Click on the “Finish” button
  25. Click on the “Create variant” button
  26. And finally click on the “Save” button

Our Article content type isn’t set up to work with revisions yet, so let’s change that:

  1. Go to /admin/structure/types/manage/article
  2. Under “Publishing options”:
    1. Uncheck “Published”
    2. Uncheck “Promoted to front page”
    3. Check “Create new revision”
    4. Check “Enable moderation of revisions”
    5. Make sure “Default moderation state” is set to “Draft”
    6. Click on the “Save content type” button

We’ll need some content to see what’s going on:

  1. Go to /node/add/article
  2. Give it a title: “Simple view test 1”
  3. Enter some body text: “This is a simple body.”
  4. Click on the “Save” button
  5. Click on the “Moderate” tab
  6. Set moderation state to “Published”
  7. Click on the “Apply” button
  8. Click on the “View published” tab to see your published content

At this point everything looks like it should.

Let’s create a new draft and change our content:

  1. Click on the “New draft” tab
  2. Change the title to “Simple view test 2”
  3. Change the body to “This was a simple body. Now it's a little more complex.”
  4. Click on the “Save” button

You’ll notice that the body text changed, as expected, but the page title and the title in our  view hasn’t.

The fix

To fix the panel’s page title

  1. Go to /admin/structure/pages/nojs/operation/node_view
  2. Click on “Content” on the left side underneath our Panel Variant
  3. For title enter “%node:title”
  4. Click on the “Update and save” button

The page’s node title now works correctly when switching between the published revision and the draft. Our view still ned to be fixed, though.

The problem is that our view is based on “Content”. We need to base it on “Content revision”, and so we can’t alter our existing view but need to create a new view.

  1. Go to /admin/structure/views/add to add a new view
  2. Give it a name, in this case “Revision view”
  3. Select: Show “Content revision” of type “Article”
  4. Deselect the “Create a page” checkbox
  5. Click on the “Continue & edit” button
     
  6. Click on the “+ Add” dropdown and select “Content pane”
  7. Add a “Contextual filter” of type “Content revision: Vid” (this will allow us to pass in the node’s revision ID instead of the node ID)
  8. Configure it to “Hide view” when no filter value is available; we’re going to pass in the value through the panel’s config pane since we can’t get it from the URL
  9. Apply (all displays)
  10. By default the view includes a “published” filter. We need to remove it in order to display the title for any revision, draft or published: Under “Filter criteria” click on “(Get the actual content from a content revision.) Content: Published (Yes)”
  11. Click on the “Remove” button
  12. Let’s also remove the revision date field so only the title remains: Under “Fields” click on “Content revision: Updated date (Updated date)”
  13. Click on the “Remove” button
  14. Under “Pane settings” click on the “Edit” link for “Argument input”
  15. Change “Content revision: Vid source” to “Input on pane config”
  16. Click on the “Apply” button
  17. Save the view

Now that we’re using a new view, we’ll have to replace it on our panels page:

  1. Go to /admin/structure/pages/nojs/operation/node_view
  2. Click on “Content” on the left side underneath our Panel Variant
  3. Use the cog in the “Center” panel to “Add content”
  4. Select “View panes” on the left
  5. Select “View: Revision view” (the new view pane we just created above)
  6. For “Content revision: Vid” enter “%node:vid”; this will pass along the node’s revision ID to the view
  7. Click on the “Finish” button
  8. Drag and move the newly added pane “View: Revision view” to just below our “View: Simple view”
  9. Click on the cog inside the old “View: Simple view” and select “Remove”
  10. Confirm that you wish to “Remove this pane”
  11. Click on the “Update and save” button

When you now look at the published version and the draft version of your Article node, all content displays correctly. Congratulations, you have fixed your site!