ElementEditor Drafts & External Content #13795
-
Hello, I'm bumping up some confusion over how drafts are managed in Garnish/JS. Specifically I'm not sure when my custom code should be triggering a draft save. Here's the basic idea of a page builder custom field that stores related components against my custom field.
Because a video is worth a thousand rambley words here's that flow: Screen.Recording.2023-10-07.at.5.44.04.AM.movNotice in that flow how saving the component triggers my own custom action/route to save the data. That doesn't touch the parent entry and doesn't create any drafts. However, once the slide out closes and I click anywhere on the screen I get an automatic So, what's the correct flow here? Should I be managing drafts when I save the component? Should I avoid that and let Craft's native Not directly related, but I did read #11349 too and it somewhat helped me understand how the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Little confused. Is the change saved, or is its updated state being hard-coded into a form input? |
Beta Was this translation helpful? Give feedback.
-
It seems like ideally, your components should be created normally right away, and assigned to the (provisional) draft, and then reassigned to the canonical entry when the draft is applied. That way you don’t need to worry about serializing the component and storing it in a hidden input. To do that, you would first need to ensure that the user is actually editing a (provisional) draft. You can do that by calling async function createComponent() {
const myField = document.getElementById('my-field-id');
const editor = $.data(myField.closest('form'), 'elementEditor');
await editor.ensureIsDraftOrRevision();
new Craft.CpScreenSlideout('keystone/components/edit', {
elementId: editor.settings.elementId,
// ...
});
}
createComponent();
|
Beta Was this translation helpful? Give feedback.
It seems like ideally, your components should be created normally right away, and assigned to the (provisional) draft, and then reassigned to the canonical entry when the draft is applied. That way you don’t need to worry about serializing the component and storing it in a hidden input.
To do that, you would first need to ensure that the user is actually editing a (provisional) draft. You can do that by calling
ElementEditor::ensureIsDraftOrRevision()
. (So long as your JS code is only getting executed when$static
isfalse
from your field’sinputHtml()
method, you don’t need to worry about it being a revision.)