Introduce a new key currentEntity in getPageData from the code component utils package

[#3565754]

This MR:

  • Introduces a new key called currentEntity in drupalSettings that provides the entity's UUID
  • getPageData() from drupal-utils now also returns the currentEntity

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.

image.png

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

Merge request reports

Loading