0

Drupal 8. When you click the "Add child page" link at a book page, you go to "/node/add/content type" where you can create and save the child page. Upon return to the book page (assuming the book navigation block is being used to display the book outline), the newly added child is not displayed (usually) in the outline until the cache is cleared and the page refreshed. Children are displayed in alphabetical order by default.

This is challenging for content editors who enter child pages because they don't see the child they've just entered when returning to the book page, and when they do see them, the children are not listed in the order of entry.

Is there a way to:

  1. Automatically clear the cache upon leaving the add child page? Or disable caching for the book navigation block, at least for users with certain roles? Or something else to allow the editor to see the added child?

  2. Automatically add the child page to the top or the bottom of the book outline? Possibly by passing a weight (in the query string?) to the add child page that's placed in the "Weight" field in the Book Outline tab at the right? (Note that the parent node id IS passed via the query string and inserted into the Book and Parent item fields in the Book Outline tab.) Or something else so the editor will know where in the outline to look for the newly added child page?

To this second question, one might say, "Just have them enter the weight when creating the child page." This works fine until they drag to reorder the outline. When that happens, Drupal assigns weights beginning with -15 to the child pages, so editors aren't sure what weight to enter.

Just trying to make the process of entering child pages for books as simple as possible so there will be fewer mistakes (especially same chapter added twice, or not at all).

0

As an answer to the first question above about updating a book outline display without clearing the cache:

Add the following to a custom .theme file (see drupal.org/docs/8/theming) to add a tag to the cached book navigation block:

function YOURTHEME_preprocess_block(&$vars)
{
  if ($vars['plugin_id'] == 'book_navigation') {
    $vars['#cache']['tags'] = ['node_type:{content-type}'];
  }
}

Then add the following to a custom .module file (see drupal.org/docs/8/creating-custom-modules) to flush the cached book navigation block whenever a node of the specified content type is changed or deleted:

use Drupal\node\NodeInterface;
use Drupal\Core\Cache\Cache;

function YOURMODULE_node_presave(NodeInterface $node)
{
  if ($node->getType() == '{content-type}') {
    Cache::invalidateTags(['node_type:{content-type}']);
  }
}

function YOURMODULE_node_delete(NodeInterface $node)
{
  if ($node->getType() == '{content-type}') {
    Cache::invalidateTags(['node_type:{content-type}']);
  }
}

Credit to @oknate for the second half of this answer. See How do you clear cache on a block when node of certain bundle added or updated? for more detail.

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.