Drupal 8: Ajax Callbacks

Portland ME - We are building a Drupal 8 module that needs some AJAX power and we were fortunate to find this blog by Mike Miles that broke it down for us.

Since we are rather new to Drupal 8 module building we could not figure out the Routing + Controller set up that the AJAX callback needs. It didn't seem right that the controller would return a render array when we only wanted a simple boolean or status code returned for our needs. Drupal.org has a great example for getting started with Routes and Controllers and one item to really take note of is the "_controller" element in the route definition. Not only is this defining the controller class, but the method as well. In this case the method is content() as indicated by the :: proceeding the method name (ExampleController::content). This will probably be very obvious to most with OOP experience but worth noting for us who are still picking it up.

Now that we understood that we define the method in the route definition, we could jump back over to Mike's tutorial and wire up a proper Ajax Reponse! Thanks Mike! In our Controller we will have a method that is built to return an AjaxResponse object like Mike's readMessageCallback method.

Happy Drupal8 Coding :)Air Foamposite One Pro

Comments (0)

About Us

We are web developers versed in front and back end technologies. We like PHP, Drupal and Wordpress for our web apps and Foundation, Bootstrap, SUSY and Sass for our User Interfaces. Contact us to be apart of your next web project.

Latest Articles

Copyright © 2021 Focal55 Inc. / All rights reserved.