Embracing the Future with Drupal 10: Introducing the Transform API
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.
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.
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
#access
This decides whether the user has access to view this transform. Defaults to TRUE. If FALSE will return an empty array.
#access_callback
Provides a callback to determine whether the user has access to view this transform.
#cache
This controls the caching of the transformation. This follows the standard CacheableMetadata from Drupal core.
#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.
#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.
#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
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.
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.
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.
Image Attribution Disclaimer: At The Drop Times (TDT), we are committed to properly crediting photographers whose images appear in our content. Many of the images we use come from event organizers, interviewees, or publicly shared galleries under CC BY-SA licenses. However, some images may come from personal collections where metadata is lost, making proper attribution challenging.
Our purpose in using these images is to highlight Drupal, its events, and its contributors—not for commercial gain. If you recognize an image on our platform that is uncredited or incorrectly attributed, we encourage you to reach out to us at #thedroptimes channel on Drupal Slack.
We value the work of visual storytellers and appreciate your help in ensuring fair attribution. Thank you for supporting open-source collaboration!