X-data

Contents

X-data (short for eXtra-data) is a smart way of dynamically extending existing content types with more fields. For instance, you may install a new app that will extend your schema with new fields.

In Content Studio, X-data is visually displayed as a new step in the form, listed below the content type’s standard fields.

The tasks and examples in this chapter are based on work done in previous chapters (starting with the sandboxes chapter). If you want to follow along with the examples, make sure you’re all caught up.

Task: SEO Metafields

The fastest way of getting to know X-data is by installing an app.

  1. Install the SEO Metafields app.

    Install SEO Metafields app
  2. Edit and add the app to your site.

  3. Immediately after adding the app to your site, the form should refresh - and add a new step "SEO Metafields":

    SEO form step and additional fields appear.

So, now you have some additional fields for the site. If you check some of your other content items, like an animal. You will see the fields are there as well.

SEO fields available for the animals content type as well.

Task: Bring your own

Similar to content types, X-data schemas have a specific naming and placement convention within your app: src/main/resources/site/x-data/<xdata-name>/<xdata-name>.xml.

  1. Create x-data schema

    This time, we’ll give you some slack, and let you choose the name and fields of the X-data yourself. However, the form needs to be wrapped in the following schema:

    Basic X-data schema
    <x-data>
      <display-name>My Xdata</display-name>
      <form>
        Your form config goes here
      </form>
    </x-data>
  2. Additionally, you need to define the injection rules for the X-data

    You do this via the site.xml.

    src/main/resources/site/site.xml
    <site>
      <x-data name="<xdata-name>" allowContentTypes="animal"/>
      <form/>
    </site>
    For more details and configuration options for X-data, visit the X-data documentation.

Once your app is redeployed, when editing the animals form, should see yet another X-data step in the form. Sweet.

Headless

As you might expect, your x-data fields are also available via the Guillotine API.

Use the following query to quickly access all x-data fields in your content.

Querying for X-data
{
  guillotine {
    query(query: "displayName = 'Lynx'") {
      displayName
      xAsJson
    }
  }
}

If you’ve added any values to your Lynx’s SEO fields, this is what you might get.

X-data results
{
  "data": {
    "guillotine": {
      "query": [
        {
          "displayName": "Lynx",
          "xAsJson": {
            "com-enonic-app-metafields": {
              "meta-data": {
                "seoTitle": "Meet the bobcat",
                "blockRobots": false
              }
            }
          }
        }
      ]
    }
  }
}

Should you require specific fields only, or wish to drill into more details, for instance by following a reference, use the typed graphQL fields instead of xAsJson. For instance like this:

X-data seoTitle field query
{
  guillotine {
    query(query: "displayName = 'Lynx'") {
      displayName
      x {
        com_enonic_app_metafields {
          meta_data {
            seoTitle
          }
        }
      }
    }
  }
}

If you made it here - you are officially equipped with an essential overview of Enonic’s content fundamental content modelling capabilities. 🙌


Contents