Drupal 10 Module Development - Fourth Edition by Daniel Sipos

Drupal 10 Module Development - Fourth Edition by Daniel Sipos

Author:Daniel Sipos
Language: eng
Format: epub
Publisher: Packt
Published: 2023-11-15T00:00:00+00:00


Query alters

Lots of things in Drupal are alterable using various hooks; queries are no different. This means that if a module writes a query such as we’ve seen before, other modules can alter it by implementing hook_query_alter(). So let’s consider an example of how this may work.

Assume the following query, which simply returns all player records:

$result = $database->select('players', 'p') ->fields('p') ->execute();

Imagine that another module wants to alter this query and limit the results to find only the players in a specific team. There is one problem. Our query has no markers that can indicate to another module that this is the one that needs to be altered. As you can imagine, there are a bunch of queries that are run in any given request, so identifying queries becomes impossible. Enter query tags.

The previous query would not be alterable because it’s not recognizable, and therefore, hook_query_alter() is not even fired on it. In order to make it alterable, we will need to add a query tag and make it identifiable. There is a simple method on the query builder for doing just that: addTag():

$result = $database->select('players', 'p') ->fields('p') ->addTag('player_query') ->execute();

Query tags are simple strings that can be read from inside a hook_query_alter() implementation. So, we could alter the query like this:

/** * Implements hook_query_alter(). */ function module_name_query_alter(Drupal\Core\Database\Query \AlterableInterface $query) { if (!$query->hasTag('player_query')) { return; } // Alter query }

The only parameter of this hook is the query object onto which we can apply our changes. It also has methods for reading the tags, such as hasTag(), hasAnyTag(), or hasAllTags(). In the previous example, we took a defensive approach and simply exited if the query was not about our player_query tagged query. I’ll come back to this later on.

Now, let’s see how we can alter this query to achieve what we set out to do:

$query->join('teams', 't', 't.id = p.team_id'); $query->addField('t', 'name', 'team_name'); $query->condition('t.name', 'My Team');

As you can see, we are doing a similar thing to what we did before when we built our joined query. We join the team table, add its name field (as a bonus), and set a condition to only return the players in a certain team. Easy peasy.

Let’s now return for a second to my remark about the defensive approach we took with this hook implementation. I personally prefer to keep methods short and return early, rather than have a bunch of unintelligible nested conditions. This is typically easy to do in an object-oriented setting. However, with procedural code, it becomes a bit more tedious as you need many private functions that are tricky to name, and even more so with hook implementations into which you might need to add more than one block of code. For example, in our hook_query_alter() implementation, we might need to add an alteration for another query later on. Also, since we return early, we need to add another condition for checking for two tags, and then some more conditions and if statements, and even more conditions (OK, rant over). From a PHP



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
SvelteKit Up and Running by Dylan Hildenbrand(6321)
Building Blazor WebAssembly Applications with gRPC by Václav Pekárek(3696)
Hands-On Application Development with PyCharm by Bruce M. Van Horn II
 Quan Nguyen(2192)
Designing Web APIs with Strapi: Get started with the Strapi headless CMS by building a complete learning management system API by Khalid Elshafie Mozafar Haider(1051)
Django 4 for the Impatient. Learn the core concepts of Python web development with Django in one weekend by G. Lim D. Correa(909)
Vue.js 3 Design Patterns and Best Practices by Pablo David Garaguso(848)
Accelerating Server-Side Development with Fastify by Manuel Spigolon & Maksim Sinik & Matteo Collina(848)
Drupal 10 Module Development - Fourth Edition by Daniel Sipos(764)
Mastering CSS Grid by Thormeier Pascal;(727)
Going the Distance with Babylon.js: Building extensible, maintainable, and attractive browser-based interactive applications using JavaScript by Josh Elster(697)
Simplifying State Management in React Native by Aleksandra Desmurs-Linczewska(617)
Java Memory Management by Maaike van Putten & Seán Kennedy(545)
Hands-On Application Development with Pycharm by II Bruce M. Van Horn;Nguyen Quan;(529)
Joomla!® Explained: Your Step-by-Step Guide (Joanne Romanovich's Library) by Stephen Burge(323)
Python & JavaScript Mastery: 2 Books In 1- Learn And Master Two Powerful Programming Languages by Alex iversion(308)
Beginning Modern JavaScript: A Step-By-Step Gentle Guide to Learn JavaScript for Beginners (Code With Nathan) by Sebhastian Nathan(261)
Understanding JavaScript Promises by Nicholas C. Zakas(247)
Create GUI Applications with Python & Qt6: The hands-on guide to making apps with Python by Martin Fitzpatrick(244)
Programming With Java by Edet Theophilus(243)
NextJS 13 and React Crash Course: Build a Full Stack NextJS 13 App with React, Tailwind and Prisma backend by Lim Greg(229)