Introduce a new key currentEntity in getPageData from the code component utils package
[#3565754]
This MR:
- Introduces a new key called
currentEntityindrupalSettingsthat provides the entity's UUID -
getPageData()fromdrupal-utilsnow also returns thecurrentEntity
Example usage:
On article 2 and the code component code filters out the current entity (article 2) from the related article using drupal-jsonapi-paramsand filter by the current entity uuid.
import { getPageData } from '@/lib/drupal-utils';
import { JsonApiClient } from 'drupal-canvas';
import { DrupalJsonApiParams } from 'drupal-jsonapi-params';
import useSWR from 'swr';
const client = new JsonApiClient();
export default function RelatedArticles({ articles, title = 'Related Articles' }) {
const { currentEntity } = getPageData();
const { data, error, isLoading } = useSWR(
[
'node--article',
{
queryString: new DrupalJsonApiParams()
.addInclude(['field_image'])
.addFilter('id', currentEntity.uuid, '<>')
.getQueryString(),
},
],
([type, options]) => client.getCollection(type, options),
);
if (error) return 'An error has occurred.';
if (isLoading) return 'Loading...';
return (
....
);
}
Edited by Harumi Jang
