Closed jgaehring closed 1 year ago
Despite having implemented the corresponding include
feature in farmOS/farmOS.js#75, I decided to go a very different route in Field Kit, adding a link()
function, which essentially checks out a single entity (one-to-one) or collection of entities (one-to-many) that is reactively "linked" to the original entity's corresponding relationship field. So I've gone ahead and changed the title of this issue accordingly.
To take an example straight from my current fork & branch of the Tasks.vue
component, given you have a log, assigned below as current
:
You can then checkout a collection of all the quantities related to that log on the quantity
field:
To add a new quantity to the log, you just call revise()
on the original log (in the below example, update()
is essentially just a wrapper around that), appending a resource identifier with a null id
to the quantity
field:
Because they're reactively linked, the quantities
collection will automatically get a reference to the same quantity, with the same type and id and default values for all other fields, appended to the same position of the collection as the log's quantity
field.
Then if you want to update an individual quantity itself, you just use the revise()
function, once again, but on the quantity reference:
Finally, and this was the tricky part, to save all the changes for both the log and any added or modified quantities related to it, you just have to commit the log itself:
Essentially, the log reference keeps a running list of dependencies
for each of its fields, and so once the time comes to commit
any changes, it checks its dependencies for any outstanding changes, commits those first, and then commits the log. This was pretty tricky, especially because Drupal's revision history module requires a whole bunch of extra Drupal metadata, a requirement that honestly makes no sense to me, but :shrug: ...
So I'm really glad I could abstract that nasty bit of boilerplate away, and each and every field module won't have to negotiate that exchange on their own
The best part about this, at least I hope :crossed_fingers:, is that it's a pattern that should work well for most relationships between entities. Images and files will still need a bit of their own added logic, which will be the entire concern for the next release, alpha.9 (Observations Module), but hopefully it should make it much easier to actually link the file entity itself to the relevant log(s).
Resolved by fc2337f.
In keeping with farmOS/farmOS.js#62 (and dependent upon it), this will probably be the best way to get quantities, files and assets related to a particular log.