Skip to content

The schema system

Drupal display modes become schemas that drive field rendering, generated once at startup and reused to narrow the JSON:API query.

After routing resolves which entity to render, the next question is which fields, in what order, with what settings. Druxt's answer is unusual: it doesn't invent a frontend field configuration, it reads Drupal's display modes. A display mode is Drupal's saved, per-view configuration of which fields appear, in what order, with which formatter and settings, managed by site builders in the admin UI.

The druxt-schema package turns view modes and form modes into schemas: plain objects describing the fields to render and the components to render them with.

An example schema

A schema for an article's teaser view mode looks conceptually like:

{
  id: 'node--article--teaser--view',
  resourceType: 'node--article',
  config: { mode: 'teaser', schemaType: 'view' },
  fields: {
    field_image: {
      id: 'field_image',
      type: 'image',          // the Drupal field formatter id
      weight: 0,
      label: { text: 'Image', position: 'hidden' },
      settings: { /* formatter settings */ },
    },
    // ...
  },
}
druxt: {
  entity: {
    query: { schema: true },
  },
},

Field components

Rendering a field means choosing a component for its formatter. The schema system maps Drupal field formatters to DruxtField* components: an image field with an image formatter becomes DruxtFieldImage, a text field with the default formatter becomes DruxtFieldTextDefault, and so on. As with everything Druxt, the chosen component is then themeable via the suggestion system: you override the rendering, not the mapping.

Why display modes?

The alternative designs all have a known failure mode: a parallel frontend field configuration drifts from Drupal, or a bespoke "fields in YAML" format reinvents what site builders already manage. Display modes come with a UI, per-bundle and per-mode overrides, third-party settings from contrib, and a decade of Drupal muscle memory. Druxt inherits all of it, at the cost of depending on display-mode config being present and correct on the backend.