Test Enterprise-Class Web CMS Scrivito Free for 30 Days
Test Scrivito Free for 30 Days
New in 1.20.0

updateReferences(mapping)

Updates all references to other CMS objects, based on a mapping function.

Scrivito.currentPage().updateReferences((refId) => if (refId !== Scrivito.Obj.getByPermalink("about-us").id()) return; return Scrivito.Obj.getByPermalink("imprint").id(); );

Updates all references to other CMS objects in every attribute, including those of widgets. The given mapping function will be applied to each referenced target CMS object ID. References are found in attributes of the types reference, referencelist, link, linklist and html.

The returned Promise resolves, after all references have been updated.

Params

  • mapping (Function) – A function that receives the CMS object ID (String) for each reference of the object, including references inside its widgets. The function is expected to return either a valid CMS object ID (String) or undefined, the latter to indicate that the reference should not be changed.

Returns

Promise<void> – Resolves as soon as all new IDs have been set.

Throws

Nothing. Errors from the callback or invalid callback results are thrown asynchronously.

Remarks

The input CMS object ID does not necessarily need to refer to an existing CMS object.

The returned CMS object ID may refer to a CMS object not (yet) created. It must be a valid object ID though, i.e. a sixteen characters hex string.

Combine the call with finishSaving if the subsequent statements must not be executed before the changes have actually been persisted.

Examples

Replaces the links pointing to a soon-to-be-deleted CMS object with their new target:

toBeDeleted = Scrivito.Obj.where('title', 'contains', 'soon deleted').first(); replacement = Scrivito.Obj.where('title', 'contains', 'linkhere').first().id(); objLinkingDeleted = Scrivito.Obj.where('*', 'linksTo', toBeDeleted).first(); objLinkingDeleted.updateReferences( (refId) => refId === toBeDeleted.id() ? replacement : undefined );

Replaces references to a different site with their corresponding destinations from the source site. Additionally waits for the changes to be persisted, using finishSaving:

gaulicSite = Scrivito.Obj.onSite('gaul'); romanSite = Scrivito.Obj.onSite('spqr'); romanCopy = gaulicSite.where('title', 'contains', 'lutetia').first(); romanCopy.updateReferences((refId) => { const oldGaulicRef = gaulicSite.get(refId); if (!oldGaulicRef) return; const correspondingRoman = romanSite.getByPath(oldGaulicRef.path()); if (!correspondingRoman) return; return correspondingRoman.id(); }); await romanCopy.finishSaving();