I've got a plugin that uses field layouts.
I was asked about Craft 4 Conditional Field support so I went away to investigate. From what I gathered so far, the toggling of conditional fields happens when a revision/draft is saved. I can see POST requests sent off to a controller action but I'm not sure how the Entry page itself is then updated without a page refresh. HTMX maybe? Also how does Craft reinitialize all the CP JavaScript to add event listeners for newly added field types?
The plugin in question uses its own "Event" class type, so it doesn't use Craft's Elements, so revisions/drafts are not supported and since revisions/draft is limited to Elements Entry, I currently can't add my "Event" class unless I do a major refactor.
So my first two questions are:
- How is the entry fields updated without a page refresh?
- How does Craft reinitialize all the CP JavaScript to add event listeners for newly added field types?
I attempted to toggle fields by using AJAX and calling a controller action to get the field layout, create the form and render the fields HTML. Then return this HTML back to the edit page and inject the HTML into the page using some more Javascript. Finally attach the event listeners again. However this approach doesn't work well for the more complex field types such as Tables, ColorInputs, Matrix etc as non of the event listeners attached seem to work. The DOM is not aware of the updated fields on the page and/or Craft has already done something fancy.
3rd question:
So how can I add Craft 4's conditional field support to a plugin? I can't find any useful documentation on Conditional fields that can help with updating plugins.
Thanks Sean