Skip to content

Instantly share code, notes, and snippets.

@Spurlos
Created February 25, 2025 16:06
Show Gist options
  • Select an option

  • Save Spurlos/80fc871ff37b6b1ad0c5d21b7c16b4b7 to your computer and use it in GitHub Desktop.

Select an option

Save Spurlos/80fc871ff37b6b1ad0c5d21b7c16b4b7 to your computer and use it in GitHub Desktop.
Paragraph entity embeds convertion to AJAX
async _loadPreview(modelElement) {
return new Promise((resolve, reject) => {
const query = {
text: this._renderElement(modelElement),
};
const ajaxSettings = {
url: Drupal.url('embed/preview/' + this.options.format + '?' + new URLSearchParams(query)),
// element: modelElement,
submit: { text: query.text },
error: function () {
reject({ label: this.labelError, preview: this.previewError });
},
beforeSend: (jqXHR) => {
jqXHR.setRequestHeader('X-Drupal-EmbedPreview-CSRF-Token', this.options.previewCsrfToken);
}
};
// Create the AJAX instance
const ajax = new Drupal.ajax(ajaxSettings);
// const originalSuccess = ajax.success;
ajax.success = function (response, status) {
Drupal.Ajax.prototype.success.call(ajax, response, status);
const label = Drupal.t('Paragraph embed widget');
resolve({ label, preview: response[3].data });
};
// Execute the AJAX request
ajax.execute();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment