decoupled_blocks
The Decoupled Blocks Powered by Paragraphs module enables frontend applications to fetch and display blocks from a Drupal site's active theme. Blocks are enhanced with injected paragraph entities, making them highly customizable and structured for headless or decoupled projects.
This module is ideal for developers working with frameworks like React and Next.js who want a clean and consistent way to retrieve and render block content.
Features
- Exposes Theme Blocks: Provides access to active theme blocks injected with paragraph entities for structured content delivery.
- API-Driven: Retrieve blocks for any page using the simple endpoint: /api/decoupled/_blocks?path=/given-path
- Customizable Field Exposure: Use the provided hook to control which paragraph fields and relationships are exposed through JSON:API specifications.
Post-Installation
Configuration Steps:
1. Enable the module via the Admin UI or Drush: drush en decoupled_blocks_paragraphs
Fetch blocks for a specific path using: /api/decoupled/_blocks?path=/your-path
Specify Your Custom Paragraph Field Name:
Implement the following hook to define the paragraph field name to be exposed
By default, the module uses field_paragraphs to retrieve data from the injected block.
/**
* Alter paragraph field name.
*/
function hook_decoupled_blocks_paragraph_field_name(&$paragraphField) {
$paragraphField = "field_paragraphs_name";
}
Customizing Exposed Paragraph Data:
Implement the following hook to define which fields should be exposed in the API response:
/**
* Alter the paragraph fields exposed by the Decoupled Blocks API.
*
* @param string $paragraphType
* The type of paragraph to filter.
* @param array &$filters
* The array of filters to apply to the JSON:API response.
*/
function hook_decoupled_blocks_filters_alter($paragraphType, &$filters) {
if ($paragraphType === "custom-paragraph-type") {
$filters = [
"fields" => [
"block_content--template_block" => "block_machine_name",
"media--image" => "name,thumbnail",
"file--image" => "filename,uri",
],
"include" => "field_logo,field_logo.thumbnail",
];
}
}The injected paragraph field in blocks must be singular
Let me know if you'd like further tweaks!