Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom forms in plugins #835

Closed
chvelkov opened this issue Oct 23, 2016 · 2 comments
Closed

Using custom forms in plugins #835

chvelkov opened this issue Oct 23, 2016 · 2 comments
Assignees
Labels

Comments

@chvelkov
Copy link

Hi,
I am building an open source plugin for galleries for grav. The plugin has a drag-n-drop interface for the Admin Panel, which allows users to create galleries, upload images to a central storage, cut & paste images, reorder, etc. I also want to take advantage of the custom forms functionality to allow users to add additional data to individual images. This is how I display forms currently:

  • The user clicks clicks on the "Edit image" button. An AJAX request is made to the server to load the form.
  • The plugin renders the custom form and sends the HTML back to the client.
  • On the client side the received HTML of the form is inserted into a remodal instance and displayed.

The problem is that I need to initialize the form once it's displayed in order for widgets like toggables, arrays, datetime fields and similar to work. Without initialization only simple text inputs work.

Looking through the code of the admin and form plugins I am unable to identify how to properly do it.
Is it possible to initialize custom forms in plugins? If so, what would be the best method to do it?

Thank you.

@flaviocopes
Copy link
Contributor

Assigning to @w00fz as he knows best about the JS forms implementation

@w00fz
Copy link
Member

w00fz commented Oct 24, 2016

What we use in Grav to deal with this is to rely on mutation events. I added an event listener mutation._grav on the body that kicks in everytime there is a mutation in the DOM which is the perfect place to look for significant changes where you might want to initialize your class.

$('body').on('mutation._grav', this._onAddedNodes.bind(this));

Have a look on how I'm doing this in the fields array, datetime, filepicker, selectize and files.

It is the same approach on all, listen to the mutation event listener, ensure it matches the changes for your field, if it does then initialize.

@w00fz w00fz closed this as completed Mar 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants