Using jsonapi_extras for custom fieldsfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In our previous lesson, we explored how to create module-specific REST and GraphQL endpoints. Today, we continue to enhance your headless Drupal application by customizing the JSON:API output using jsonapi_extras to add and manipulate fields.

Introduction to jsonapi_extras

jsonapi_extras is a Drupal contributed module that provides additional flexibility in managing JSON:API requests. It allows you to configure the JSON:API output, offering capabilities like exposing and renaming fields, and customizing metadata.

Installing jsonapi_extras

Before diving into configuration, ensure you have the jsonapi_extras module installed. Use Composer for installation:

    composer require drupal/jsonapi_extras
    

Then, enable the module:

    drush en jsonapi_extras -y
    

Configuring jsonapi_extras for Custom Fields

Step 1: Access the Configuration

After enabling the module, navigate to Admin > Configuration > Web services > JSON:API Extras. You will find options to create a new configuration for your content types or entities.

Step 2: Add Custom Fields

To add custom fields, select the entity type and bundle you wish to configure. For instance, to add a custom field to the Article content type:

1. Click on Add new configuration.

2. Choose Content as the entity type and Article as the bundle.

3. Click Save to create the configuration.

Step 3: Map and Rename Fields

jsonapi_extras allows you to rename or hide fields in the JSON response. Access the Fields section within your configuration:

  • Map Fields: Here, list custom fields you wish to output in the JSON response.
  • Rename Fields: If you need to provide a different label for a field, you can set it under this section, helping align API output with frontend naming conventions.

Step 4: Adjust Additional Options

In addition to fields, you can:

  • Change Resource Name: Useful for adjusting the API path to better reflect your site's nomenclature.
  • Control Meta Information: Customize meta tags within JSON responses for additional clarity.

Testing Your Configuration

To ensure your JSON:API configuration is correct, test it using a tool like Postman or directly in the browser. Navigate to a URL endpoint, such as:

    https://yourdomain.com/jsonapi/node/article
    

Check that the JSON output reflects your jsonapi_extras configurations, including custom fields and renamed attributes.

Practical Example

Let's say we added a field summary to our Articles:

    {
        "data": {
            ...
            "attributes": {
                "summary": "This is a custom field added via jsonapi_extras",
                ...
            }
        }
    }
    

This implementation not only enhances the flexibility of your data output but also ensures that frontend developers can work seamlessly with the API.

Conclusion

Using jsonapi_extras to manage and customize your JSON:API output allows your headless Drupal application to deliver precise data tailored to your requirements. This customization broadens the API's adaptability and aligns it with application specifics.

Teaser for Next Lesson

Stay tuned for our next lesson where we will explore defining custom GraphQL types and resolvers. This will further enhance the depth and specificity of your API, offering unmatched customization possibilities.