diff --git a/packages/nuxt-ripple/mapping/base/index.ts b/packages/nuxt-ripple/mapping/base/index.ts index 74dc3f7f96..666ef70065 100644 --- a/packages/nuxt-ripple/mapping/base/index.ts +++ b/packages/nuxt-ripple/mapping/base/index.ts @@ -23,7 +23,7 @@ import { includes as sidebarSiteSectionNavIncludes } from './sidebar-site-section-nav/sidebar-site-section-nav-mapping.js' import TidePageMeta from './page-meta.js' -import { TidePageApi } from '@dpc-sdp/ripple-tide-api' +import { TidePageApi, getSiteSection } from '@dpc-sdp/ripple-tide-api' export const tidePageBaseMapping = ({ // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -65,13 +65,8 @@ export const tidePageBaseMapping = ({ status: 'moderation_state', topicTags: topicTagsMapping, siteSection: async (src, tidePageApi: TidePageApi) => { - // The section/site id comes from the drupal 'route' api - const siteId = tidePageApi.sectionId - // With the correct site/section id, we can now choose the correct site data from 'field_node_site' - const siteData = src.field_node_site?.find((site) => { - return `${site.drupal_internal__tid}` === siteId - }) + const siteData = getSiteSection(tidePageApi.sectionId, src) if (!siteData) { return null diff --git a/packages/nuxt-ripple/mapping/base/sidebar-site-section-nav/sidebar-site-section-nav-mapping.ts b/packages/nuxt-ripple/mapping/base/sidebar-site-section-nav/sidebar-site-section-nav-mapping.ts index ad3d3c3810..0d29d03b13 100644 --- a/packages/nuxt-ripple/mapping/base/sidebar-site-section-nav/sidebar-site-section-nav-mapping.ts +++ b/packages/nuxt-ripple/mapping/base/sidebar-site-section-nav/sidebar-site-section-nav-mapping.ts @@ -1,17 +1,16 @@ -import { TidePageApi } from '@dpc-sdp/ripple-tide-api' +import { TidePageApi, getSiteSection } from '@dpc-sdp/ripple-tide-api' export const map = async (src, tidePageApi: TidePageApi) => { if (!src.field_show_site_section_nav) { return null } - // The section/site id comes from the drupal 'route' api - const siteId = tidePageApi.sectionId - // With the correct site/section id, we can now choose the correct site data from 'field_node_site' - const siteData = src.field_node_site.find((site) => { - return `${site.drupal_internal__tid}` === siteId - }) + const siteData = getSiteSection(tidePageApi.sectionId, src) + + if (!siteData) { + return null + } // Finally, this site data will contain a reference to the menu we need to fetch const menuData = siteData.field_site_main_menu diff --git a/packages/ripple-tide-api/src/utils/mapping-utils.ts b/packages/ripple-tide-api/src/utils/mapping-utils.ts index 1d474c60de..d35c93f816 100644 --- a/packages/ripple-tide-api/src/utils/mapping-utils.ts +++ b/packages/ripple-tide-api/src/utils/mapping-utils.ts @@ -37,6 +37,14 @@ interface RawCardImage { data: RawCardImageData[] } +type tidePageSitePartial = { + field_node_site: [ + { + drupal_internal__tid: number + } + ] +} + /** * @deprecated Need to make a decision on whether we proxy images or use direct url */ @@ -170,6 +178,38 @@ export const getSiteKeyValues = (key: string, src: any) => { } } +/** + * @description returns the site section id from either the passed section ID or the page src sites. Selects the last site as per existing logic. + */ +export const getSiteSectionId = ( + sectionId: string, + src: tidePageSitePartial +) => { + if (sectionId) { + return `${sectionId}` + } + if (src.field_node_site && src.field_node_site.length > 0) { + return `${src.field_node_site.slice(-1)[0].drupal_internal__tid}` + } + return null +} + +/** + * @description returns the correct site section from the page sites data + */ +export const getSiteSection = (sectionId, src) => { + const siteId = getSiteSectionId(sectionId, src) + + if (!siteId) { + return null + } + + // With the correct site/section id, we can now choose the correct site data from 'field_node_site' + return src.field_node_site?.find((site) => { + return `${site.drupal_internal__tid}` === siteId + }) +} + export default { getImageFromField, getLinkFromField,