I've finished a first version of fQuery. In case you missed my earlier blog entry, here's the scoop:
fQuery is a selecting mechanism inspired by jQuery, which makes writing (amongst other things) Drupal form_alter hooks much easier. Specifically, it means you don't need to manually iterate over the entire $form tree just to find certain elements.
All you do is pass it a selector based on the familiar CSS syntax, and you get an array of matching elements back. There's plenty of voodoo magic under the hood, but the usage is ridiculously simple:
<?php
$query = f("fieldset.collapsible", $form);
foreach ($query as &$element) {
...
}
?>fQuery supports a variety of selectors from CSS1, 2 and 3. Some selectors which were rather difficult to implement, or which were of little practical use, were not included.
Downloads and documentation are on the fQuery page.
Note that it is easy to extend fQuery to provide useful results for other Form API-like structures, e.g. the node view array. There's lots of potential here.
Check it out and post your comments!

Ridiculously Cool
I know I'll be using this.
Thanks!
inc not ideal
the problem with a .inc file is that every module that wants to use this has to include it in own package. this will lead to function name conflict if two modules both want to use fquery. i think a module is better.
i will give it some testing and feedback.
Module
You're right. I converted it into a simple module that modules can depend on. It's now in Drupal.org Contrib and should appear on the project page soon.
independent of Drupal?
I'm sure I would use this lib if it were independent of Drupal. I don't use Drupal, but I love jQuery and would love to have the php equivalent.
From reading the code it seems that element_children() is the only function it needs from "elsewhere". And what is the $form prototype?
Form API
This code only works with Drupal Forms API arrays, which must have a specific structure.
Post new comment