3 * Form-based in-place editor. Works for any field type.
6 (function($, Drupal, _) {
10 * @augments Drupal.quickedit.EditorView
12 Drupal.quickedit.editors.form = Drupal.quickedit.EditorView.extend(
13 /** @lends Drupal.quickedit.editors.form# */ {
15 * Tracks form container DOM element that is used while in-place editing.
22 * Holds the {@link Drupal.Ajax} object.
31 * @param {object} fieldModel
32 * The field model that holds the state.
33 * @param {string} state
34 * The state to change to.
36 stateChange(fieldModel, state) {
37 const from = fieldModel.previous('state');
44 if (from !== 'inactive') {
53 // If coming from an invalid state, then the form is already loaded.
54 if (from !== 'invalid') {
73 this.showValidationErrors();
82 * A settings object for the quick edit UI.
84 getQuickEditUISettings() {
88 fullWidthToolbar: true,
94 * Loads the form for this field, displays it on top of the actual field.
97 const fieldModel = this.fieldModel;
99 // Generate a DOM-compatible ID for the form container DOM element.
100 const id = `quickedit-form-for-${fieldModel.id.replace(
105 // Render form container.
106 const $formContainer = $(
107 Drupal.theme('quickeditFormContainer', {
109 loadingMsg: Drupal.t('Loading…'),
112 this.$formContainer = $formContainer;
114 .find('.quickedit-form')
116 'quickedit-editable quickedit-highlighted quickedit-editing',
118 .attr('role', 'dialog');
120 // Insert form container in DOM.
121 if (this.$el.css('display') === 'inline') {
122 $formContainer.prependTo(this.$el.offsetParent());
123 // Position the form container to render on top of the field's element.
124 const pos = this.$el.position();
125 $formContainer.css('left', pos.left).css('top', pos.top);
127 $formContainer.insertBefore(this.$el);
130 // Load form, insert it into the form container and attach event handlers.
131 const formOptions = {
132 fieldID: fieldModel.get('fieldID'),
135 // Reset an existing entry for this entity in the PrivateTempStore (if
136 // any) when loading the field. Logically speaking, this should happen
137 // in a separate request because this is an entity-level operation, not
138 // a field-level operation. But that would require an additional
139 // request, that might not even be necessary: it is only when a user
140 // loads a first changed field for an entity that this needs to happen:
142 reset: !fieldModel.get('entity').get('inTempStore'),
144 Drupal.quickedit.util.form.load(formOptions, (form, ajax) => {
145 Drupal.AjaxCommands.prototype.insert(ajax, {
147 selector: `#${id} .placeholder`,
151 .on('formUpdated.quickedit', ':input', event => {
152 const state = fieldModel.get('state');
153 // If the form is in an invalid state, it will persist on the page.
154 // Set the field to activating so that the user can correct the
156 if (state === 'invalid') {
157 fieldModel.set('state', 'activating');
159 // Otherwise assume that the fieldModel is in a candidate state and
160 // set it to changed on formUpdate.
162 fieldModel.set('state', 'changed');
165 .on('keypress.quickedit', 'input', event => {
166 if (event.keyCode === 13) {
171 // The in-place editor has loaded; change state to 'active'.
172 fieldModel.set('state', 'active');
177 * Removes the form for this field, detaches behaviors and event handlers.
180 if (this.$formContainer === null) {
184 delete this.formSaveAjax;
185 // Allow form widgets to detach properly.
186 Drupal.detachBehaviors(this.$formContainer.get(0), null, 'unload');
188 .off('change.quickedit', ':input')
189 .off('keypress.quickedit', 'input')
191 this.$formContainer = null;
198 const $formContainer = this.$formContainer;
199 const $submit = $formContainer.find('.quickedit-form-submit');
200 const editorModel = this.model;
201 const fieldModel = this.fieldModel;
203 // Create an AJAX object for the form associated with the field.
204 let formSaveAjax = Drupal.quickedit.util.form.ajaxifySaving(
207 other_view_modes: fieldModel.findOtherViewModes(),
212 function cleanUpAjax() {
213 Drupal.quickedit.util.form.unajaxifySaving(formSaveAjax);
217 // Successfully saved.
218 formSaveAjax.commands.quickeditFieldFormSaved = function(
224 // First, transition the state to 'saved'.
225 fieldModel.set('state', 'saved');
226 // Second, set the 'htmlForOtherViewModes' attribute, so that when this
227 // field is rerendered, the change can be propagated to other instances
228 // of this field, which may be displayed in different view modes.
229 fieldModel.set('htmlForOtherViewModes', response.other_view_modes);
230 // Finally, set the 'html' attribute on the field model. This will cause
231 // the field to be rerendered.
233 fieldModel.set('html', response.data);
237 // Unsuccessfully saved; validation errors.
238 formSaveAjax.commands.quickeditFieldFormValidationErrors = function(
243 editorModel.set('validationErrors', response.data);
244 fieldModel.set('state', 'invalid');
247 // The quickeditFieldForm AJAX command is called upon attempting to save
248 // the form; Form API will mark which form items have errors, if any. This
249 // command is invoked only if validation errors exist and then it runs
250 // before editFieldFormValidationErrors().
251 formSaveAjax.commands.quickeditFieldForm = function(
256 Drupal.AjaxCommands.prototype.insert(ajax, {
258 selector: `#${$formContainer.attr('id')} form`,
262 // Click the form's submit button; the scoped AJAX commands above will
263 // handle the server's response.
264 $submit.trigger('click.quickedit');
270 showValidationErrors() {
272 .find('.quickedit-form')
273 .addClass('quickedit-validation-error')
275 .prepend(this.model.get('validationErrors'));
279 })(jQuery, Drupal, _);