hoppinger / rest_normalizations
Drupal module to extend REST resources
Installs: 14 032
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 6
Forks: 1
Open Issues: 2
Type:drupal-module
- dev-master
- 10.x-dev
- 10.1.x-dev
- 9.x-dev
- 9.1.x-dev
- 1.9.2
- v1.9.1
- v1.5.1
- 1.5.0
- 1.4.0
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-bugfix/warning-for-paragraph-entity
- dev-feature/field-target-normalizer
- dev-fix/drupal-update
- dev-documentation
- dev-file_urls
- dev-language_links
This package is auto-updated.
Last update: 2024-12-08 17:12:56 UTC
README
The Rest Normalizations module for Drupal 8 extends the default Drupal 8 normalizations for the REST API. It adds properties that are helpful in a headless architecture and helps with embedding sub-entities in entities.
Installation
composer require hoppinger/rest_normalizations
Version compatibiliy
Version 1.4.0 and up are only compatible with Drupal 8.7.0 and up, and older versions are not compatible with Drupal 8.7.0 and up.
Usage
Rest Normalization module helps to embed the entity referenced in the content, by altering the REST API.
For example, The Entity reference field in the REST Output provides the target_id, target_type and URI. In some cases, we need more data regarding the referenced entity other than the provided data. Rest Normalization helps to solve this problem by overriding the default values and embedding the entity data into the response.
Create a .php file in the module directory /src with the following content. Replace the <target_identifier>
with the project-specific entity reference field that needs to be embedded into the API response (for example: node-story_overview-field_featured_story
)
The <target_identifiers> can be entityreference fields, Paragraphs, File, Image, Video, Media, Taxonomyreference field.
The filename must be similar to the module name i.e., ModuleNameServiceProvider.php and must be placed in the src folder.
namespace Drupal\module_name; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceProviderBase; use Symfony\Component\DependencyInjection\Reference; class ModuleNameServiceProvider extends ServiceProviderBase { public function alter(ContainerBuilder $container) { if ($container->hasParameter('rest_normalizations.target_identifiers')) { $target_identifiers = $container->getParameter('rest_normalizations.target_identifiers'); $target_identifiers[] = <target_identifier>; #e.g., node-story_overview-field_featured_story $container->setParameter('rest_normalizations.target_identifiers', $target_identifiers); } }
By default, media, file and paragraph are already added as the target identifiers.