Drupal 8/9: Facets AJAX commands

Faceted search

Possible solutions

  • Disable AJAX on the faceted search 💩
  • Listen to the facets_filter JavaScript event 😐
  • Alter the response object of the facet AJAX callback 👍

The JavaScript event

  • Add a JavaScript file
  • Add an event listener
  • Execute (jQuery) AJAX call(s) to the Drupal back-end
  • Update the content with for example jQuery(‘.selector’).html(response);

Altering the response object

/facets-block-ajax
  1. I didn’t need an extra (unnecessary) request to the back-end
  2. I only had to add some PHP code to make this work. Everything simple and centralized, just as I like my code to be.

The best way forward

Overriding the facet route

Adding a custom controller

Defining a custom event

Adding event subscribers

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Backend & DBs

The Little Guide for OOP in JS

Web & Mobile Programming

How to Make a GET Request with the React Axios Library

Tiny Angular pipe to make any function memoizable

Simple flash notifications Vue composition api + Tailwindcss + mitt

The MERN Challenge: Building the FCC Twitch App using MongoDB, Express, Reactjs, and Nodejs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Robin Ingelbrecht

Robin Ingelbrecht

More from Medium

PHP fundamentals — part 1

Multiple PHP versions in single XAMPP on Windows

Web server on windows for local development

Configure Xdebug + PHPstorm on wsl2