Create a Calculated Field Containing an Item's Own _id Concatenated With the _ids of All Referenced Items in a Multi-Reference Field.

My goal is to use an afterquery data hook (or, if that proves impossible, perhaps queryReferenced() or inlcude() to create a calculated field (of type array) that concatenates an item’s _id with the _ids of the referenced items in a multi-reference field. In the specific example below, the multi-reference field happens to refer back to the same collection, but I think the same approach should work regardless of which collection is referenced in the multi-reference field.

I have very little coding experience, so I beg your indulgence and greatly appreciate any help you may be able to offer.

Here’s a detailed description of what I’m trying to accomplish:

I have created a collection called “Activity” and have set up a many-to-many relationship between the Activity collection and itself by creating a multi-reference field called “IsASubActivityOf”, which references items in the same collection.

Let’s imagine I create an item in that collection and name the item “2024 Summer Program” (by typing that string into the “title” field). For simplicity’s sake, let’s say that the item is assigned an _id of 1,000,000.

Next, I create another item in the Activity collection and call it “Conference 1”. This item is assigned an _id of 20,000. In the “IsASubActivityOf” field of that item, I select “2024 Summer Program” (which is _id 1,000,000) from the dropdown menu. An additional new field is automatically created in the Activity collection (which I define as “HasTheseSubActivities”), and, of course, that field for the referenced item (_id 1,000,000) is automatically populated with the _id of the referring item (which, in this case, is _id 20,000).

Next, I create items of _id 1,000 and _id 3,000 in the Activity collection and call them “Workshop A” and “Workshop B”, respectively. In the “IsASubActivityOf” field of each of those items, I select “Conference 1” (which is _id 20,000) from the dropdown menu. The “HasTheseSubActivities” for the referenced item (_id 20,000) is automatically populated with the _ids of the two referring items (which, in this case, are _id 1,000 and _id 3,000, respectively).

My goal now is to use an afterquery data hook to create a new calculated field (of type array) called something like “SelfPlusAllSubActivities” in the Activity collection. That array in that calculated field would includes the item’s own _id PLUS the _ids of all of the item’s subActivities and their subActivities and their subActivities and so on without limitation, In other words, I want to be able to nest Activities within other Activities to any level and without limitations, such that any Activity can have zero or any number of sub-Activities and can be a Sub-Activity of zero or any number of other Activities. And I want all of the subActivities of a given Activity’s subActivities (and the subActivities of those and so on) to appear automatically (dynamically) in the “SelfPlusAllSubActivities” calculated field as subActivities of all activities that are higher in the hierarchy, as it were. I don’t want to have to add them multiple times manually.

I believe that the JavaScript concat() method may be the way to accomplish this.

The challenge I’m encountering is that I can’t figure out how to convert the value of a Wix multi-reference field into an array of the referenced _ids so I can concatenate it with the corresponding item _id using the Javascript Concat Method.

Something like this…

var self = item_id (a string)
var subActivities = HasTheseSubActivities (an array)
var selfPlusSubActivities = self.concat(subActivities);

If this isn’t possible, I imagine that there is a way to accomplish something similar by using include() or queryReferenced() in page code.

I’m hoping to end up with something like the following:

Activity_id = 10
HasTheseSubActivities: null
IsASubActivityOf: 100
SelfPlusSubActivities = [10,]

Activity_id = 11
HasTheseSubActivities: null
IsASubActivityOf; 100
SelfPlusSubActivities = [11,]

Activity_id = 12
HasTheseSubActivities: null
IsASubActivityOf; 100
SelfPlusSubActivities = [12,]

Activity_id = 13
HasTheseSubActivities: null
IsASubActivityOf; 100
SelfPlusSubActivities = [13,]

Activity_id = 23
HasTheseSubActivities: null
IsASubActivityOf; 200
SelfPlusSubActivities = [23,]

Activity_id = 25
HasTheseSubActivities: null
IsASubActivityOf; 200
SelfPlusSubActivities = [25,]

Activity_id = 31
HasTheseSubActivities: null
IsASubActivityOf; 300
SelfPlusSubActivities = [31,]

Activity_id = 36
HasTheseSubActivities: null
IsASubActivityOf; 300
SelfPlusSubActivities = [36,]

Activity_id = 38
HasTheseSubActivities: null
IsASubActivityOf; 300
SelfPlusSubActivities = [38,]

Activity_id = 100
HasTheseSubActivities: [10, 11, 12, 13]
IsASubActivityOf; 1,000
SelfPlusSubActivities = [100, 10, 11, 12, 13]

Activity_id = 200
HasTheseSubActivities: [23, 25]
IsASubActivityOf; 3,000
SelfPlusSubActivities = [200, 23, 25]

Activity_id = 300
HasTheseSubActivities: [31, 36, 38]
IsASubActivityOf; 3,000
SelfPlusSubActivities = [300, 31, 36, 38]

Activity_id = 1,000
HasTheseSubActivities: [100, 10, 11, 12, 13]
IsASubActivityOf; 20,000
SelfPlusSubActivities = [1,000, 100, 10, 11, 12, 13]

Activity_id = 3,000
HasTheseSubActivities: [200, 300]
IsASubActivityOf; 20,000
SelfPlusSubActivities = [3,000, 200, 232, 25, 300, 31, 36, 38]

Activity_id = 20,000
HasTheseSubActivities: 1,000 and 3,000
IsASubActivityOf; 1,000,000
SelfPlusSubActivities = [20,000, 1,000, 100, 10, 11, 12, 13, 3,000, 200, 232, 25, 300, 31, 36, 38]

Activity_id = 1,000,000
HasTheseSubActivities: 20,000, 30,000, 40,000, and 60,000
IsASubActivityOf; null
SelfPlusSubActivities = [20,000, 1,000, 100, 10, 11, 12, 13, 3,000, 200, 232, 25, 300, 31, 36, 38, 30,000, 40,000, 60,000]

Any help will be greatly appreciated!