Skip to content

Instantly share code, notes, and snippets.

View ummdorian's full-sized avatar

Dorian Damon ummdorian

View GitHub Profile
@mparker17
mparker17 / ajax_callback.md
Created August 17, 2018 20:24
Drupal 8 Form API #ajax.callback

'#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 .module file, 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 calls someFunction() 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'],