'#ajax' => ['callback' is weird.
Looking at examples from Drupal core, there seems to be a bunch of different ways you can call it...
-
'callback' => 'some_global_function'is the simplest and always seems to work, but the function it calls has to be defined in a.modulefile, not a class. -
'callback' => [$this, 'someFunction']works sometimes, but you get a 'cannot serialize database service' error if any of$this' properties (or anything they reference) involves a database connection. -
'callback' => [static::class, 'someFunction']works, but callssomeFunction()in a static context, meaning you can't use$this->someService->doSomething().This is equivalent to, and has the same problems as:
'callback' => [get_class($this), 'someFunction'],'callback' => '\Fully\Qualified\Class::someFunction','callback' => get_called_class() . '::someFunction', and'callback' => __CLASS__ . '::someFunction'.
-
'callback' => '::someFunction'is supposed to work in a dynamic context, but it only works on objects thatimplement FormInterface— so if yourhook_form_alter()calls another class to do some of its work and that class wants to define an AJAX callback, it cannot (at least, not out-of-the-box)