Hot answers tagged

22 votes

Show node IDs along with titles in Entity reference Autocomplete list

Install the Views and Entity Reference modules, create a new View and Add a Entity Reference Display: Then Add in the fields the content title and the nid, click in the nid and check Exclude from ...
Adrian Cid Almaguer's user avatar
20 votes
Accepted

How to load content in a div with ajax?

I've come up with a working solution. It might not be the best approach but it works. You need to create your routing file: my_module.routing.yml diocese_core.path_with_data: path: 'path-with-data' ...
Adrian Cid Almaguer's user avatar
19 votes

How to load content in a div with ajax?

This is exactly what you need. var endpoint = Drupal.url('modal/get-content'); Drupal.ajax({ url: endpoint }).execute(); where endpoint is the URL of your route. So you have controller for your ...
Alex Kuzava's user avatar
  • 1,076
18 votes
Accepted

How to Trigger Existing, Non-Form Ajax from JavaScript Event

If you don't want to ajaxify a link by adding the class use-ajax you can trigger an Ajax request programmatically: Drupal.ajax({url: endpoint}).execute(); This creates an Ajax object with the ...
4uk4's user avatar
  • 99.3k
16 votes

How to prevent an Ajax form from submitting

I found the solution here. // Add submit handler to form.beforeSend. // Update Drupal.Ajax.prototype.beforeSend only once. if (typeof Drupal.Ajax !== 'undefined' && typeof Drupal.Ajax....
jrockowitz's user avatar
  • 4,623
15 votes

How to do a hard redirect when in Ajaxified form?

In my case (D8), I just had to implement the RedirectCommand in my AjaxSubmit : Code of the submit button : $form['submit_' . $this->uniqueIdentifier] = [ '#type' => 'submit', '#value' =&...
romain ni's user avatar
  • 827
13 votes
Accepted

How do I add a "Add more" button?

It works !! Try this <?php namespace Drupal\fapi_example\Form; use Drupal\Core\Form\FormStateInterface; class AjaxAddMore extends FormBase { public function buildForm(array $form, ...
Shreya Shetty's user avatar
12 votes
Accepted

Why doesn't the AJAX callback call the class contructor?

Your problem is actually something different entirely. It is your use of private. Don't use it, use protected. Drupal has a special logic to prevent services from being serialized but it doesn't work ...
Berdir's user avatar
  • 83.1k
11 votes
Accepted

Clear form fields after AJAX submit

@Berdir's answer helped me to figure out the solution. We should indeed clear the user input, but we can not simply just say $form_state->setUserInput([]), because there are some system values, ...
milkovsky's user avatar
  • 5,334
11 votes
Accepted

Manually invoking Ajax Commands when an ajax callback is not an #ajax form callback

Like @Gaius I don't know exactly what @Jaypan wants to achieve. This example might help to make this clearer: var response = JSON.parse(content); // Create a Drupal.Ajax object ...
4uk4's user avatar
  • 99.3k
10 votes

Status messages with AJAX

For Drupal 8 it's $response = new AjaxResponse(); $status_messages = array('#type' => 'status_messages'); $messages = \Drupal::service('renderer')->renderRoot($status_messages); if (!empty(...
Tim Bozeman's user avatar
10 votes

How to display more than 10 items in link widget autocomplete?

If you can live with overriding all of the autocomplete limits, you can override a core service in Drupal 8; The service you need to override is here in core.services.yml: entity....
oknate's user avatar
  • 14.2k
9 votes

Clear form fields after AJAX submit

You are looking for https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormStateInterface.php/function/FormStateInterface%3A%3AsetUserInput/8.2.x. The values are the validated ...
Berdir's user avatar
  • 83.1k
9 votes
Accepted

How to display more than 10 items in link widget autocomplete?

I suppose overriding EntityAutocompleteMatcher will affect all autocomplete form elements on your site. So that I would create a new entity selection plugin instead because it is more granular ...
ya.teck's user avatar
  • 5,624
9 votes
Accepted

AJAX callback on form item generated by AJAX

The Link 4k4 posted in the discussion of the question gives the solution: Create a callback function (named by the #ajax['callback']). This is generally a very simple function which does nothing ...
schneidolf's user avatar
9 votes
Accepted

Refresh View in block using js

The way you construct your myViews variable seems to be the reason this doesn't work for you. What worked for me was changing this: myViews = '.view-dom-id-' + element.settings....
Stefanos Petrakis's user avatar
9 votes
Accepted

What difference between declaration of an ajax callback function with $this and without it?

The ::method as a string is a special feature of the form API. It's basically a shorthand for [$this, 'method'] and does almost exactly the same. See FormState::prepareCallback There is one ...
Berdir's user avatar
  • 83.1k
9 votes

Ajax submission leads to => An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (2 MB) that this server supports

By debugging the code I found the solution. The error was triggered from core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php if ($exception instanceof BrokenPostRequestException &&...
Abouhassane Abdelhamid's user avatar
9 votes
Accepted

Drupal behaviors not attached after Ajax load

Behaviours are fired after a Drupal AJAX call - a call to jQuery.ajax isn’t the same thing. Fortunately it’s easy to attach them yourself, just add this in the success callback when you’ve finished ...
Clive's user avatar
  • 167k
9 votes

LogicException: The database connection is not serializable - when I try to open any "gear" on checkout flow page?

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 ...
Daniel Steindl's user avatar
8 votes

controller function use with ajax

WARNING: The following code contains serious security vulnerabilities, and should not be used in a production site without modification. After lots of searching finally, I got the solution. I would ...
sandipshirsale's user avatar
8 votes
Accepted

Calling a JS function after managed file has completed uploading

This is tricky in D8, as the ajax code does not offer any hooks or callbacks after the function has run. The solution is to override the ajax callback function that the managed_file element uses, ...
Jaypan's user avatar
  • 17.4k
8 votes
Accepted

Error handling with Drupal.ajax()

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 ...
MrD's user avatar
  • 6,208
7 votes

How do I get multiple references from the autocomplete?

The comma is used to separate multiple references, but looking at the code in Name::valueForm() I would say the form element needs to use a property more. $form['value'] = array( '#type' => '...
apaderno's user avatar
  • 97.4k
7 votes
Accepted

Loading block via ajax

You can use the #ajax attribute to load main part of the block via Drupal ajax. namespace Drupal\generator\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\generator\Form\ProjektForm; ...
Eyal's user avatar
  • 3,246
7 votes
Accepted

Custom authentication provider returns 403 for _access: 'TRUE'

Routes need to specify the authentication providers they want to support. By default, that's session, so authenticating with something else is not allowed. You do this by specifying _auth: ['...
Berdir's user avatar
  • 83.1k
7 votes
Accepted

Using #states in a form with checkboxes

To use state for checkboxes element, you should target the specific element by JQuery selector, all elements they have the name="field_select_shipping" with value attribute different so try to target ...
berramou's user avatar
  • 7,175
7 votes
Accepted

Open ajax dialog with JavaScript

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', ...
4uk4's user avatar
  • 99.3k
6 votes

Is it possible to replace more than one form element (wrappers) triggered by only one #ajax trigger element?

Drupal 8 alternate syntax use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; class name extends FormBase{ function ajax_example_simplest(array $form, FormStateInterface &...
Johann's user avatar
  • 131

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