Can I use this?

This feature is available since Webiny v5.40.0.

What you’ll learn
  • what is a Content Entry Traverser
  • how to extract values from any content entry

Overview
anchor

Content entry traverser is a utility which allows you to traverse a content entry, and run a callback for every available value. If you’ve ever used Abstract Syntax Trees (AST), then this should be very familiar. A traverser is created using a given content model. Using the content model definition, the traverser can then reliably visit every key/value pair in the give content entry, and give you its context: field definition, current key path within the entry, etc.

A tool like this is very handy when you need to process a content entry based on some criteria, extract some field values, apply asynchronous transformations (e.g., translations using an external system), and so on.

Using a Traverser
anchor

Let’s imagine you want to extract text, long-text, and rich-text field values from an article content model, each time a content entry is saved:

Extract values on each content entry update

What we did here is as follows:

  • created a Context plugin for the GraphQL API
  • subscribed to the onEntryAfterUpdate lifecycle events
  • requested a traverser object for a specific content model
  • defined a list of field types we’re interested in
  • ran a traverser on the entry values which we received in the event payload
  • collect path and value of the fields we’re interested in

path is an interesting property, because it does a lot of thinking for you. If you have a content model with nested objects, dynamic zones, maybe even arrays of objects, the path will point to an exact depth and array index of the current value being visited.

Here’s an imaginary output you could get from a relatively complex content model:

Example Data

Now you can further process the extracted data, send it to remote APIs, etc. The exact value path allows you to easily set the value back to its original position in the content entry, granted the entry doesn’t change in the meantime (in which case you might set your data to a wrong index).