Skip to content

Layouts and theming

Preview layouts

List the Nuxt layouts the preview may use in the layouts option:

druxt: {
  nodePreview: {
    layouts: ['default', 'preview'],
  },
}

Then, on the consumer in Drupal, add one layout name in each Preview layouts box. Put default first, because the preview opens in the first one. Add &layout=[layout] to the target URL, after ?vm=[view_mode] and before the #.

With automatic registration, the module sends the layouts option to Drupal in place of those boxes. List default first there too, for the same reason. When layouts is empty, the module sends no layouts, and Drupal keeps the consumer's own boxes.

This needs Nuxt's default history router. With router: { mode: 'hash' }, the route itself goes after the #, so the page can read neither the layout nor the preview address.

The page uses the layout that ?layout= names, but only if layouts lists it. If not, the page uses the default layout. A preview URL cannot pick any other layout.

Each listed layout must be a file in the site's layouts/ folder that renders <Nuxt />. A bare layout suits content that sits inside other pages. It has the site's type and spacing, but no header or footer.

A site that picks layouts somewhere else can call $druxtNodePreview.layout(name). It makes the same check.

Theming the preview

A live page often adds a title or a container around the node. The preview gets the same from a wrapper component, picked like any Druxt wrapper. For a node--doc_page previewed in full, the page uses the first of these that exists:

  1. DruxtNodePreviewDocPageFull
  2. DruxtNodePreviewDocPage
  3. DruxtNodePreviewDefault

If none of them exists, the node renders in Druxt's plain wrapper.

The preview page's own component is DruxtNodePreviewRoute. So a bundle called page can have a DruxtNodePreviewPage wrapper like any other.

A wrapper gets any of these props that it declares:

  • document: the whole JSON:API preview document.
  • entity: the previewed resource, with attributes such as the title.
  • mode, type and uuid.

Its default slot renders the node with DruxtEntity:

<!-- components/druxt/node-preview/DocPageFull.vue -->
<template>
  <article class="prose">
    <h1>{{ entity.attributes.title }}</h1>
    <slot />
  </article>
</template>

<script>
export default {
  // The props it does not declare would otherwise land on <article>.
  inheritAttrs: false,
  props: {
    entity: { type: Object, required: true },
  },
}
</script>

A wrapper named by bundle alone, such as DruxtNodePreviewDocPage, applies to every view mode. Put page parts such as a title in the <Bundle>Full wrapper, as above, or check the mode prop.

Components that fetch their own data

A component that reads the Druxt store shows the unsaved data. A component that fetches its data again, such as paragraphs at a revision, shows the saved content instead. Have it read the resource from the Druxt store during a preview:

const preview = this.$store.state.druxtNodePreview || {}
if (preview.active && this.$druxtNodePreview.has(type, id)) {
  // Read the resource from the Druxt store here.
  return
}

Here type and id are the resource's JSON:API type and UUID. $druxtNodePreview.has() is true only for a resource in the preview document. A saved copy of the resource, or one in another language, may be in the store too, so finding it there proves nothing. The preview stores its resources under the route's language, as DruxtEntity reads them. A resource the document lacks still needs its fetch, so add deeper paths to the include option.

The store has two values. active is true while a preview is in the store. uuid is the previewed node's UUID then, and null otherwise. A component can compare uuid with its own node's to tell whether the preview is for it.

Skipping the fetch is not enough on its own. A component that shows only what it fetched would then show nothing. A fetch that started before the preview data arrived can also finish after it. Its result must then be dropped, so check active again when it returns.

When the editor leaves the preview, the module removes the preview data from the store. The next page loads the saved content.

The module stores the data under the route's langcode meta. DruxtEntity reads it there when it has no langcode prop.