I have a use case where it would be really helpful to show a linked entity in a rich text field.
Context
We build a content hub for solopreneurs in Fibery. They are not used with working in Fibery / databases / all the advanced stuff etc. So we try to make everything as simple as possible.
Use case
User creates a Content item in the content database
We have an automation button to ‘repurpose the content’
When user clicks, we create a duplicate
Question
It would be really helpful to show in the description which entities are linked, instead of showing the database.
Because one piece of content can be duplicated many times, and therefor it can be a child or parent or both. It will then show like this and that can feel a bit overwhelming for our customers:
I think this should be somehow possible with the Append function in the automation but I’m getting stuck with the markdown template for the linked entity items
Here’s a script that will append #mentions to linked items.
It can be triggered when an item is linked or unlinked, since it relies on clearing any previous #mentions and replacing them with the new needed ones.
It assumes that you have a many-to-many self-relation, with fields called ‘Parent’ and ‘Child’.
I hope it’s useful, or at the very least, inspirational.
const fibery = context.getService('fibery');
const schema = await fibery.getSchema();
for (const entity of args.currentEntities) {
//get DB ID for later
const dbID = schema['typeObjects'].filter((obj) => obj.name == entity.type)[0]['id'];
//get info from entity
const entityPlus = await fibery.getEntityById(entity.type, entity.id, ['Description', 'Parent', 'Child']);
//get the current document contents
var doc = await fibery.getDocumentContent(entityPlus['Description']['Secret'], 'md');
//strip any previous linked items
const position = doc.search(/\n\n\*\*Linked items:\*\*/);
var newDoc
if (position > 0) {
newDoc = doc.substring(0, position);
}
else {
newDoc = doc;
}
//append mentions of linked parents or children, if any
if ((entityPlus['Parent'].length > 0) || (entityPlus['Child'].length > 0)) {
newDoc = newDoc + ('\n\n**Linked items:**');
for (const parent of entityPlus['Parent']) {
const mention = '\n[[#^' + [dbID] + '/' + [parent.Id] + ']]';
newDoc = newDoc + mention;
}
for (const child of entityPlus['Child']) {
const mention = '\n[[#^' + [dbID] + '/' + [child.Id] + ']]';
newDoc = newDoc + mention;
}
}
await fibery.setDocumentContent(entityPlus['Description']['Secret'], newDoc, 'md');
}