craftcms / docs

Documentation for Craft CMS, Craft Commerce, and other official products.
https://craftcms.com/docs
39 stars 150 forks source link

Element Saving for Drafts, Revisions, Multi-Site need documentation for Plugin Development #36

Open BenParizek opened 4 years ago

BenParizek commented 4 years ago

Since the move to make Drafts and Revisions Elements, it has been increasingly confusing for the number of scenarios custom Elements and Field Types need to support (measured by bug reports, time spent on client Yii modules dealing with Elements/Field Types, and curse words). In most cases, I think the solution is easier than I'm making it but the lack of documentation on how to even think about the problem makes it challenging to even know if you're considering all the possible scenarios for Drafts, Revisions, and Multi-Site.

Using X-Debug to troubleshoot is a challenge too as even if you are trying to just watch a single method involved in the save behavior the code ends up hitting that method countless times for things like auto-save, SCENARIO_ESSENTIALS, SCENARIO_LIVE, drafts, revisions, multi-site propagation, and more. Throw in a few variations on the Element settings or different Translation Methods and you have even more scenarios to make sense of as you try to figure out a simple question such as what is a reliable condition I can use to make sure I only trigger something when an Element is saved in the Live Scenario for the first time?, which should not be a complicated question to answer. It is. Or at least feels like it.

To illustrate my confusion here's a spreadsheet I made while trying to figure out how to save a custom field type with a separate data table: https://share.barrelstrength.co/YEu1nwBK

The spreadsheet is trying to make sense of the number of different times Elements were saving in 6 different scenarios, which each save Elements multiple times.

This doesn't even get into examples of Elements saving for Multi-site or scenarios where Element saves get triggered from the console, queue, migrations, or failed validation.


Ideal documentation would identify (and give us a checklist for) ALL of the different scenarios we need to plan for, give us a framework on how to think about the problem (at least for custom Elements and Field Types), and clearly show an example of how our code can identify each scenario.

mattstein commented 3 years ago

Relevant Stack Exchange answer: https://craftcms.stackexchange.com/a/38046/22