Currently there are no advanced filter modules (like CMF) for Drupal 7's /admin/content/node page and other users have been bring up usability issues with this page.
You can almost alter this page to add your own bespoke filters without touching the core. Using hook_form_alter you can add new form elements, and using hook_query_alter you can alter the the db query that lists the nodes.
However, in hook_query_alter there is no way to know which query to alter to change the filter query.
There is a really simple fix for this, specifically add a tag http://drupal.org/node/310077 to the filter query.
So in /modules/node/node.admin.inc around line 409:
$query = db_select('node', 'n')->extend('PagerDefault')->extend('TableSort');
is changed to:
$query = db_select('node', 'n')->extend('PagerDefault')->extend('TableSort');
$query->addTag('node_admin_filter');
Then in your own module, do something like:
function MYMODULE_query_alter(QueryAlterableInterface &$query) {
if ( $query->hasTag('node_admin_filter') && isset($_SESSION['node_overview_filter_ext']) ) {
$str = str_replace(' ', '%', $_SESSION['node_overview_filter_ext']);
$query->condition('title', '%' . $str . '%', 'LIKE');
}
}
where $_SESSION['node_overview_filter_ext'] contains the additional filters.
Comment | File | Size | Author |
---|---|---|---|
#5 | 1191660.patch | 486 bytes | ParisLiakos |
Comments
Comment #1
Jon_B CreditAttribution: Jon_B commentedHere's a patch...
Comment #2
WilliamB CreditAttribution: WilliamB commentednice thanks
any chance you could add the code you used to add the filter in hook_form_alter too please?
And how did you make the new filter go in Session $_SESSION['node_overview_filter_ext'] ?
I can't get the added filter to bring value in hook_query_alter.
Comment #3
droplet CreditAttribution: droplet commented1. please remove line SPACE :)
2. against D8.x and backport
Comment #4
WilliamB CreditAttribution: WilliamB commentedSorry?
Comment #5
ParisLiakos CreditAttribution: ParisLiakos commentedComment #6
ParisLiakos CreditAttribution: ParisLiakos commentedbot is happy.RTBC
Comment #7
loominade CreditAttribution: loominade commentedsubscribe
Comment #8
webchickFixing tag.
Sounds like a reasonable use case to me. But before proceeding with this, I'd love to get a sanity-check from one of the database system maintainers about the usage of the DB tag system here and whether it's appropriate: chx, crell, DamZ, etc. I could see this opening a can of worms a bit by creating inconsistency around developer expectations on what sorts of queries get tags, and what tags those are.
Incidentally, another approach to this is hook_menu_alter() and just replace the page callback with one of your own devising. Views effectively does this to completely override the page output with its own stuff.
Comment #9
ParisLiakos CreditAttribution: ParisLiakos commentedThanks for the suggestion webchick, i guess i will do what i want with views,till this gets committed (and if)
Comment #10
Jon_B CreditAttribution: Jon_B commentedHi webchick. See your point - there definitely needs to be consistency on where the tag system is used. To me though, the tagging system seems to be a pretty powerful and simple bit of functionality (and maybe a little under-used?). I will be interested in seeing what decision is made.
Comment #11
Alan D. CreditAttribution: Alan D. commentedI have marked #1244504: Add drupal_alter to node_filters() and provide for query_alter to react to those filters as a duplicate. This patch added the query tag and introduced a new filter alter hook (which is a duplicate of #450666: Filter DB Extender).
Love to get this into the next release. Could someone ping "chx, crell, DamZ, etc" to push this forward?
Comment #12
Alan D. CreditAttribution: Alan D. commentedWithout patching core, this was my current workaround (which shows how unstable things are if you are not overriding the entire page callback / form):
Comment #13
Reg CreditAttribution: Reg commentedIgnore, this found the answer.
Comment #14
Alan D. CreditAttribution: Alan D. commented@Reg [Edited for context] Regarding the usage of this tag. This is only really visible via the hook_query_alter() and hook_query_node_access_alter(). It is a tag that is hard coded so that module coders can filter the results based on this.
Comment #15
Reg CreditAttribution: Reg commentedYou replied faster than I could revoke the question. Thanks. I was just reading through all the links in the post and came across what you mentioned.
Comment #16
Reg CreditAttribution: Reg commentedI see two issues here. First is making the filters extensible on the content management page, the 2nd is having a "tag filter" on the same page.
I think there is no question that it would be useful to have filtering on this page extensible so that has my vote.
The issue of specifically adding a tag filter I think is a little more involved and I would like to see that particular filter added to the core. The reason being is that when I first started using D7 not too long ago I came across a Content Type ("Article" I think) that had tagging already on it "out of the box". I saw value to that and started using the tagging almost immediately. However when I went to the Content Management page I saw no way of identifying those tagged pages with the existing filters. So the Content Management page doesn't adequately reflect what is being given "out of the box" which needs to be properly addressed as well.
Ultimately, we ended up carefully naming the pages so we could tell what was tagged with what... which is tagging the same page in two different ways for the same thing... and that makes no sense for anything except what it was, a workaround.
Comment #17
Reg CreditAttribution: Reg commentedRe: #0 & #12, since hook_query_alter() can be used for any query I would probably structure the code something like this:
This way the structure of your code cleanly reflects the catchall nature of the hook. Of course if you are creating a module to only extend the query form and nothing else then the case statement calling a separate function is overkill in which case probably just ignore this.
Comment #18
robertom CreditAttribution: robertom commented@Reg: you could implement directly hook_query_TAG_alter
Comment #19
Garrett Albright CreditAttribution: Garrett Albright commentedI presume that this issue is no longer relevant for Drupal 8, as the content page is now a View and can be altered in the standard Views ways.
The patch in #5 still applies for Drupal 7. Do we need a test for it?
Comment #20
David_Rothstein CreditAttribution: David_Rothstein commentedThe content page isn't a view in Drupal 8 unless the Views module is turned on, as far as I know (i.e. same as Drupal 7) but I think the difference is that the fallback page doesn't have filters. If so, there's nothing really equivalent to the problem described in this issue, and the page is generated by a totally different entity-listing method anyway so it would be altered in a different way.
We don't need a test for adding a tag. And @webchick's comment in #8 was more than two and a half years ago, so if anyone wanted to complain about this use of tags they've had plenty of time :) (Personally, I think it's OK, but maybe I'm biased because I've run into a problem very similar to this one in the past.)
Given that, I committed this to 7.x - thanks!