Hot answers tagged

8

You can using like jQuery ajax. Because Drupal.ajax({}).execute() is a function and return the jqXHR. /** * Execute the ajax request. * * Allows developers to execute an Ajax request manually without specifying * an event to respond to. * * @return {object} * Returns the jQuery.Deferred object underlying the Ajax request. If * ...


4

I don't think you can attach multiple ajax callbacks to a form element. But you could override the callback by extending the core element: /src/Element/MyManagedFile.php <?php namespace Drupal\mymodule\Element; use Drupal\file\Element\ManagedFile; use Drupal\Core\Ajax\AlertCommand; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\...


4

I experienced the same problem. It's not a commerce issue, and there is no general rule which module causes the problem. The problem probably occurs, because one of your checkout panes depend on a service, which has an indirect dependency to Drupals database connection -- Drupals database connection is not serializable and this causes the error. It's not ...


4

Use the SettingsCommand $settings=['ajax_forms_test' => ['foo' => 42]]; /* An array of key/value pairs of JavaScript settings. */ $merge=TRUE; /* (Optional) Booleen Whether the settings should be merged into the global drupalSettings. */ $response->addCommand(new SettingsCommand($settings,$merge)); So your code would become something like this: ...


4

You have to add ajax commands to the ajax response, using the unrendered render array: $response->addCommand(new ReplaceCommand('#form-container', $renderable)); BTW you can only return a Drupal ajax response if you have triggered an ajax request via ajax.js. Outside of form submits you usually do this by adding the class use-ajax to a link. For custom ...


4

Thanks to Clive for his help ! To active the autocomplete, I should re-attach the autocomplete behavior after added my new element. nodeSelectorWidget: function (args) { // Adding the autocomplete field var createField = function() { var input = document.createElement('input'); input.classList.add('form-autocomplete'); ...


4

Since Drupal 8 the form object instantiated with buildForm() is not preserved between the request rendering the form and the first Ajax request. So be prepared that buildForm() is called again and has to produce the exact same result. When you get data from $form_state this is not the data you expect from the first buildForm() because this is never cached. ...


3

Looks like the culprit was how I was adding the name rows to the form. Wrong: $form['names'][] = $row; Correct: $form['names'][$key] = $row; The reason it was "working" after the first submit, was that the replaced form had appended token id's to the form, so the replacement could not occur on the #testform id, as it no longer existed. Here's ...


3

I recommend the Easy Email module. It combines with Swiftmailer and Mailsystem, allowing for creating and sending HTML emails, using templates you can build with a ckeditor right in the interface. Then you can send mails from your module using the standard Drupal mail service, and it will take care of turning it into an HTML email. The video on the top page ...


3

You can trigger Ajax in custom javascript. See How to Trigger Existing, Non-Form Ajax from JavaScript Event In your case an Ajax dialog: var ajaxSettings = { url: '/node/1', dialogType: 'modal', dialog: { width: 400 }, }; var myAjaxObject = Drupal.ajax(ajaxSettings); myAjaxObject.execute();


3

This is a gotcha of AJAX forms. When you load an AJAX form via AJAX it loses its AJAX functionality. In order to keep it working, for the form that is getting loaded via ajax, you need to add a url and options keys to #ajax /* * Implements hook_form_alter() */ function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) { // I ...


3

In Drupal ajaxComplete() is not required, as Drupal executes each iteration of Drupal.behaviors.*.attach() whenever an ajax call is complete. This function receives an argument, context, which on page load contains the entire contents of the document, and on AJAX calls contains the new part of the DOM being inserted into the page. This piece of the document ...


2

Ah, I just figured out from core/misc/dialog/dialog.ajax.js that I need wrapper with class form-actions. <div class="form-actions"> <a href="mailto:test@example.com" class="button">Send email</a> </div>


2

Remove the wrapper key $form['elements']['manufacturer']['#ajax'] = [ 'callback' => 'formChanged', 'event' => 'change', ]; Your callback public function formChanged(array &$form, FormStateInterface $formState){ $form['elements']['form_factor']['#value'] = 0; $form['elements']['model']['#value'] = 0; $response = new \Drupal\Core\...


2

Instead of returning a single render array (of all or part of your form) from your callback, you will need to return an AjaxResponse that contains a ReplaceCommand for each piece of the form you want to replace: For example, in your callback: $response = new AjaxResponse(); $response ->addCommand(new ReplaceCommand('#edit-form-factor', $...


2

If you want to send POST parameters add them to elementSettings.submit: elementSettings.submit = { foo: 'bar' }; and remove the GET parameters from elementSettings.url.


2

This works for me: $element = NULL; if (!empty($triggering_element['#ajax']['element'])) { $element = \Drupal\Component\Utility\NestedArray::getValue($form, $triggering_element['#ajax']['element']); } // Element not specified or not found. Show messages on top of the form. if (!$element) { $element = $form; } $response = new \Drupal\Core\Ajax\...


2

Core does not provide an HTML format for non-dialogs, but you can create your own custom format. mymodule/js/custom-ajax-loader.js: (function ($, Drupal) { Drupal.behaviors.myCustomAJAXStuff = { attach: function (context, settings) { $("a.use-custom-ajax", context).once('mymodule-ajax').each(function(i, ajaxLink) { const $...


2

It turns out the code above was working fine -- the problem was that the site was using a Bootstrap subtheme, and Bootstrap was configured to override the standard dialog behavior. Turning off Bootstrap's dialogs solved the problem, enabling both width and title settings.


2

There are two errors in the code shown in the question. It's only the form builder that can add options to form elements. Differently, you will get the error you quoted. An invalid selection has been identified. Please contact the site administrator. What the AJAX callback does is simply returning the form element that needs to be changed. In your case, ...


1

Ok, this was just a stupid mistake in a another javascript. There the jQuery method .off() was used to remove event listeners which had the effect to remove the Ajax event listener.


1

In general the Internal Page Cache is a rather simple solution. If you need more features replace it with Varnish or a CDN. Most of them provide similar features, like for example ESI to serve dynamic blocks if you can't use Javascript. I think most people would use Javascript in this case, though, and there is a fully cacheable solution. You can deliver all ...


1

Quick written code: <?php /** * Form. */ function sample_luhn_form($form, &$form_state) { // I don't recommend inline js, prefer usual module js file, attach via $form['#attached'] or in module info file // Also I am use jQuery instead of $, but your shown (function ($) { }jQuery)); is preferred, I ...


1

Swiftmailer in combination withmail system let you send html mail if correctly configured. It's decently documented, the very short guide is: go to: admin/config/system/mailsystem, set as formatter and sender swift mailer, use the front\whatever theme as theme for mail (optional) set the formatter\sender for the specific module go to admin/config/...


1

I doubt that the array you've found really assigns 2 callbacks. It's rather an array defining a class and a method for a single callback: 'callback' => ['Drupal\foo\BarClass', 'ajaxCallback'] In fact, you can only set one callback in an #ajax element. If you want to override a callback you have to include the overridden callback in your code: Drupal\foo\...


1

I tried to do the same in D7 a while ago and I discovered a JS only solution is not possible. You need to add an ajax command to the Drupal callback. Here's a similar question to yours. This one is about the upload button and you want the remove button. In D7, the upload and remove button were handled by the same function and when I look at the D8 class of ...


1

If you want to do more than simply return new markup, then your response should be an instance of Drupal\Core\Ajax\AjaxResponse, to which you can append one or more AJAX commands via the addCommand() method. A simplified version of this would be: use Drupal\Core\Ajax\AjaxResponse; $response = new AjaxResponse(); $response->addCommand(new ReplaceCommand('#...


1

Is there any problem with the way I attach the library to AjaxResponse that may cause the broken layout? I guess so. This is how core does it when its an AJAX response, it attaches it to the form like this: $form['#attached']['library'][] = 'my_module/library_name'; $ajax_response->setAttachments($form['#attached']);


1

Since you're programmatically setting a modal with $response->addCommand(new OpenModalDialogCommand($this->t('Test'), $modal_form, $dialog_options)); For the form that is in the modal, Drupal needs to know what URL it should post the AJAX to and be told that its an AJAX request because this only gets set automatically when the url key doesn't have a ...


1

I had the same issue. It turned out every time a paragraph is added the validation method runs. In your case, echo is illegal and it stops the following process. Try replacing it with php error_log or Devel debug functions.


Only top voted, non community-wiki answers of a minimum length are eligible