Embracing the Future with Drupal 10: Introducing the Transform API

In the ebb and flow of software, Drupal reminds us: whether monolithic or headless, everything comes full circle.
three pupas: 2010 was a boon year for these butterflies in my garden. I had a dozen chrysalis in all manner of morphs at any one time. In this image you can see the new green chrysalis coloration, one that’s about ready to emerge (the clear one), and a butterfly that’s already come out. They will hang for hours and dry their wings and are, in fact, quite fragile.
Unsplash

With the wave of digitization sweeping across industries, it is imperative for businesses to adopt platforms that are not only robust and scalable but also flexible. The world of CMS (Content Management Systems) has witnessed many innovations, and Drupal stands out as one of the forerunners, especially with its recent Drupal 10 release.

Drupal 10, celebrated for its unmatched scalability and extensibility, introduces a plethora of innovative features, elevating user and developer journeys alike. Standouts include the intuitive role access control on blocks and the rejuvenated layout builder. Furthermore, themes like Claro and Olivero bring a refreshing aesthetic. With its groundbreaking decoupled menus and improved content management, Drupal 10 encapsulates modern CMS evolution, although harnessing its full potential necessitates new tools and refined strategies. Enter the Transform API.

Transformation dashboard on content type: Drupal 10 Transform API Module
Transformation dashboard on content type

Why Transform API?

The Transform API is not just another module – it is a bridge between the traditional and the modern. While Drupal has its roots in serving HTML content, the trend has seen a shift towards headless or decoupled architectures where content is served as JSON. This is where the Transform API shines.

For developers accustomed to Drupal's entity system, view modes, and templates, the Transform API offers a seamless pathway to morph these into JSON, through configuration or code. It is like acquiring fluency in a new language, all while using the grammar you already mastered.

But here is the crux: while the JSON:API module tends to deliver raw entity data for decoupled systems, demanding the frontend to process images and data post-receipt, Transform API leans heavily on Drupal's innate capabilities to deliver data that is virtually ready-to-use. Imagine having images served in diverse formats suitable for responsive designs without the frontend lifting a finger.

Moreover, the workflow of JSON:API module starkly contrasts the process of building a conventional Drupal application with a theme. On the other hand, Transform API's workflow mirrors how you would typically develop Drupal applications. It integrates familiar elements such as blocks (transform blocks), view modes (transform modes), and formatters (transformers), along with the likes of lazy_builders (lazy_transformers) and preprocess (transform_alter) techniques.

The Transformer is equivalent of the core renderer service, but instead able to take transforms and transform arrays and turn them into JSON.

In essence, both JSON:API and Transform API address the same challenge, but the latter does it by celebrating and leveraging the Drupal way. By doing so, it effortlessly prepares existing projects for a headless transformation, endorsing the belief that transitioning to modern architectures needn't require abandoning tried-and-true methodologies.

Example showing transform blocks in Drupal 10 Transform API module
Example showing transform blocks

Transform Arrays

Transform arrays can be both associative and numerical in PHP. When transformed into JSON: 

  • Associative arrays become JSON objects.
  • Numerically indexed arrays become JSON arrays.

Example:

PHP Array:

php
$transformation = [
    "type" => "example",
    "links" => [
        [
            "url" => "",
            "title" => "example.com"
        ]
    ]
];

Resulting JSON:

json
{
    "type": "example",
    "links": [
        {
            "url": "",
            "title": "example.com"
        }
    ]
}

Controls

The next thing to know is that any associative key that starts with “#“ are used to control aspects of the transformation or used to provide information about the transformation to any alter hooks or post processors.

Here are some globally available transformation controls that you can supply in a transformation array

  1. #access

    This decides whether the user has access to view this transform. Defaults to TRUE. If FALSE will return an empty array.

  2. #access_callback

    Provides a callback to determine whether the user has access to view this transform.

  3. #cache

    This controls the caching of the transformation. This follows the standard CacheableMetadata from Drupal core.

  4. #collapse

    This is a boolean that tells the transformer that if this array only has one member, it can be collapsed into a single value. This is particularly useful if the transformation is just a value that needs to be returned.

  5. #lazy_transformer

    This allows you to add a callback that will be called after caching but before the response has been sent. This is equivalent to lazy builders in render arrays. This can be useful if your transformation includes some very dynamic content that cannot be cached.

  6. #pre_transform

    This provides a callback that alters the transform before transformation takes place. Any key that starts with “#“ will not be in the final JSON response, but can be accessed in alter hooks and lazy transformers.

Features briefly

  1. Efficient Redirect Handling:

    Traditional redirects can be cumbersome in a headless setup. The Transform API simplifies this with its RedirectResponseSubscriber, which provides detailed JSON responses for redirects. 

  2. Utility Functions:

    With the Transformer service, developers can access a plethora of functions that assist in the content transformation process. Whether it's sorting child elements or determining if an element should be transformed, this API offers a holistic solution. 

  3. Aligned with Drupal 10's Innovations:

    The API seamlessly integrates with the latest updates of Drupal 10, ensuring that users can leverage the new features to their maximum potential. While JSON:API offers a blanket solution for rendering Drupal content in JSON format, the Transform API provides a nuanced, tailored approach, especially suited to the advancements brought about by Drupal 10.

As the digital landscape evolves, so do the tools we use. The Transform API, in alignment with Drupal 10's core updates, is set to be a game-changer for developers and businesses aiming to stay ahead in the digital race. Embrace the future, and let the transformation begin.

Note: The vision of this web portal is to help promote news and stories around the Drupal community and promote and celebrate the people and organizations in the community. We strive to create and distribute our content based on these content policy. If you see any omission/variation on this please let us know in the comments below and we will try to address the issue as best we can.

Related Organizations

Advertisement Here

Upcoming Events

Latest Opportunities

Advertisement Here