Just like form_id specific alter hook, lets add a hook for each tag. This helps us to isolate code into smaller functions and uses the registry a bit better. I'd like for this to go in before we convert all node_access queries.

Comments

Anonymous’s picture

bookmark

Crell’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new2.87 KB

I'm fine with this. Attached patch just moves one of the alter unit tests to its own function as a test mechanism, and still passes, so I'm pushing up to RTBC.

Crell’s picture

StatusFileSize
new2.05 KB

And this one doesn't have my htaccess file in it...

dries’s picture

This introduces a second mechanism. Do we want to remove the first mechanism?

moshe weitzman’s picture

We don't. We'd like to keep a way for modules to listen to all alterable queries.

moshe weitzman’s picture

I should add that hook_form_alter() follows the same pattern we want to use here. This patch introduces consistency.

Crell’s picture

Removing the main query_alter() hook would make it impossible or very difficult to do multi-tag-based or non-tag-based alteration, just like not having form_alter() would make node forms or "all forms" more difficult.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

I can see how this patch would be very nice, and Moshe is correct that it is consistent with the way hook_form_alter_X() is done.

a) It doesn't make sense to me why you are pulling out just that one check into its own hook implementation, when any of those would also suffice. It makes it look at first glance like there's a hole in the testing code, until you read much further and see it there as a separate function. I'd prefer to not "cheat", and instead make a separate test entirely to test this new functionality.

b) I'd prefer to see that second function (tag-specific alter hook) documented like we document preprocess functions, for consistency. So how about:

* Implementation of hook_query_alter_TAG().

c) Do we need to benchmark this, or is the performance impact relatively small?

moshe weitzman’s picture

a) Not sure I understand. Do you want a separate .test file? A separate class? I think both of these would result in a re-install of Drupal by simpletest. We should avoid this unless necessary.

b) That documentation would no longer hyperlink the function on api.drupal.org and this implementation would no longer be listed as a Reference. I'm not completely opposed to what you say, but there are downsides too. If we do proceed with this change, it should be hook_query_TAG_alter()

c) No need. We never benchmarked the addition of a hook without implementations before. And with the code registry, this is even less important.

webchick’s picture

Nah, for a) I don't mean anything too fancy at all. Just another testX function specifically for testing a tag-specific alter hook rather than the global one.

Something like, in database_test.module, add an alias like "tag_specific_alter_hook" and in database_test.test, have a testTagSpecificAlterHook, probably in DatabaseAlter2TestCase, checking to make sure it's there. It's just a simple test to make sure the hook keeps firing through the ages.

Your points on b) are true, but are moot if we also document the tag-specific hook in core.php, which will happen when I set this CNW after committing and say "Docs please!" ;) This is a must IMO; I don't even think anyone who doesn't read form.inc on a regular basis is aware that hook_form_alter() works this way too... I'll file a second issue to document that one as well, if there isn't one already.

The docs can basically be a single sentence of description and then @see hook_query_alter().

And yes, hook_query_TAG_alter(). Sorry!

webchick’s picture

Hm. This is something worth considering with this patch:
#330784: With hook_form_$form-id_alter() module's weight might be insignificant

I can imagine this messing up forms pretty bad. I'm pretty terrified to think about how that same behaviour could mess up modules governing access control. ;)

moshe weitzman’s picture

@webchick - It think that issue is just a variation on the usual problem that not all modules can be last. Thats not a reason to skip implementing tag specific alter hook here. Perhaps we choose to swap the order of the alters in both issues although thats really splitting hairs IMO.

terrified? a bit dramatic IMO.

webchick’s picture

Dramatic yes, but worth pondering. For now, I guess we just document the hell out of it along the lines of #331832: Document hook_form_FORM_ID_alter().

Crell’s picture

The correct solution to both APIs is chx's hook weighting endeavors. That is not something that we will be able to solve in the DB system specifically, or in FAPI, so it's not really a concern here. We can document it as appropriate in a follow-up patch.

moshe weitzman’s picture

Status: Needs work » Needs review
StatusFileSize
new4.53 KB

Rerolled.

Added docs so it is explicit that our removeRange test is also a test of hook_query_TAG_alter(). Added docs for both hook_query_alter() and hook_query_TAG_alter() to system.api.inc.

Crell’s picture

Status: Needs review » Reviewed & tested by the community

#15 looks fine to me. It doesn't split off a tag-specific-check-only test, but I don't see how we can do that in a non-contrived fashion (more contrived than most unit tests are by nature) so I'm OK with it.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for the docs in system.api.php, but you're missing the most important part -- an example of how it works! :)

There has to be a reason you're adding this hook, so stick a few lines in each of the functions that illustrate why one would use it.

After that, I'm good to go on this one.

moshe weitzman’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new6.15 KB

Now that the node_query_alter() got in, this patches changes the its name to node_query_node_access_alter() as it is a good example of a tag specific alteration.

I have added @see lines to both hook_query_alter() and hook_query_TAG_alter() pointing to the new node_query_node_access_alter(). Furthermore, I pasted the contents of node_query_node_access_alter() into hook_query_TAG_alter() as it is our best example code for this hook. Hope this satisfies the request for example code.

dries’s picture

Status: Reviewed & tested by the community » Needs work

Actually, no. I think the example could use some more documentation. Specifically, I would add some inline code comments to explain different steps.

Either way, seeing this example code makes one eyebrow raise -- the code, and the metadata handling, is a bit of a hack. Is that something we can clean up? Why do you need to specify 'op' and what is 'op'? Why can't we use isSelect() or isUpdate() or something more clean than this meta data? It looks a bit redundant to me.

+    if (!$op = $query->getMetaData('op')) {
+      $op = 'view';
+    }
Crell’s picture

Moshe, hadn't we discussed breaking the node_access tag up into separate tags for each op rather than sticking that into metadata?

moshe weitzman’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new6.83 KB

Hmm. Seems like we are in a quandary where we want to discuss the new node access implementation - yet this issue is just about an alter by tag feature. Anyway, i added a few more comments to node_query_node_access_alter() and to new the sample code.

@Crell - we might have discussed that - I can't recall. To be honest, I am not displeased by the use of metadata here. Even if we use separate tags for the different ops, we still carry the 'account' as metadata.

Crell’s picture

It looks from the patch like the only place the function differs by $op is the last line, and it's just a string replacement, so I guess I'm OK with it being a single tag.

We should also update the handbook page to mention specifically that the node_access tag needs an $account metadata. I can see that being non-obvious. That's separate from committing this patch, though.

moshe weitzman’s picture

You actually don't have to pass $account in the common case where you mean "current user". Thats assumed by node_access_grants().

Crell’s picture

Hm. The override ability should then be documented to tell people they don't need to pass $account most of the time. :-)

dries’s picture

Status: Reviewed & tested by the community » Needs work

Committed the last patch to CVS HEAD. I'm marking this 'code needs work' (i) because this needs to be documented in the upgrade documentation and (ii) because of Crell's comment #24. Thanks!

moshe weitzman’s picture

Status: Needs work » Fixed

Added links to api pages in the DBTNG part of the update docs.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.