Download & Extend

On a hook, theme hook, or theme template page, list functions that invoke this hook/theme

Project:API
Version:6.x-1.x-dev
Component:User interface
Category:feature request
Priority:major
Assigned:jhodgdon
Status:closed (fixed)

Issue Summary

I'm not sure it's possible, but... Currently, when you're on a function page, you can see a list of the functions that call the function you are looking at. It would be really cool if the same thing worked for hooks: that if you were on hook_foo(), there would be a list of all the places where a function used module_invoke('foo') etc.

Comments

#1

Category:task» feature request
Priority:normal» major

#2

Title:On a hook page, list functions that invoke this hook» On a hook, theme hook, or theme template page, list functions that invoke this hook/theme

Come to think of it, this could also be good for theme functions and theme templates.

I think it actually wouldn't be too hard to do. During parsing, there is already some logic that finds all functions called during a block of code, and saves those references. It just needs to be expanded so that if it finds things like:
- module_invoke_all('field_attach_delete', $entity_type, $entity); ==> saves hook_field_attach_delete as ref.
- module_invoke($storage_info['module'], 'field_storage_delete', $entity_type, $entity, $fields); == saves hook_field_storage_delete as ref.
- module_implements('field_access') ==> saves hook_field_access as ref.
- drupal_alter('element_info', $cache); ==> saves hook_element_info_alter as ref.
- _field_invoke('delete', $entity_type, $entity); ==> saves hook_field_delete as ref.
- theme('item_list', array('items' => $items)) ==> saves theme_item_list and item-list.tpl.php as refs.
- maybe some other specific Drupalisms here, like node_invoke?

Here's the spot where this is done, in parser.php:

          foreach (api_parse_function_calls($docblock['code']) as $function_name) {
            api_reference($docblocks[0]['branch'], 'function', $function_name, $docblock['did']);
          }
        }

The api_parse_function_calls function is what does the actual work.

#3

See also #294218: Turn hook names in theme() and module_invoke() into links which is doing similar things at render time.

#4

Status:active» postponed

I think we should postpone this until we figure out if #1405914: Make code link/reference creation more orderly and rational can address it.

#5

Sample problem met today: module_hook_info() is documented as an implementation of hook_hook_info() because the name format matches the format a hook implementation would have, but it not such an implementation, but code from includes/

#6

That is true -- there will be occasional "false positives", but we decided that having the list (and keeping the code simple) was of larger benefit than the small price of having some false positives in the list.

#7

Similar false positives could probably be avoided quite simply: there are no hooks in /includes, so matches in that directory could be weeded out.

There would probably remain a few other similar false positives within modules, themes or profiles, but significantly less. I have to agreee that it is better to have the list and false positives than not have the list at all, though?

#8

I don't think we want to build assumptions about the Drupal Core directory structure into the API module, but that is a possibility. The problem is that it might not be stable throughout future versions of Drupal, and the API module is supposed to be capable of indexing all of them.

#9

Status:postponed» active

The stuff listed in #2 is being found by the parser now, since #1405914: Make code link/reference creation more orderly and rational has gone in. So this is ready to be worked on now. And by the way the issues discussed in #5-#8 are not really related to this feature request.

#10

Assigned to:Anonymous» jhodgdon

It's now time to do this!

#11

Here's a patch for this, including #1404718: Make "functions that call this" show hook_menu() callback usage of functions fix too, in case my computer crashes before I finish testing... lots of refactoring too... details coming when I get it finalized...

AttachmentSize
api-references.patch 37.36 KB

#12

Status:active» fixed

OK, this is fixed - above patch plus a couple of test tweaks. Notes:

Basically, in-code links and back-references are now the dual of each other:
- On a regular function page, you get links to separate pages for functions that call this function (as before), and now also functions that reference this function name as a string (issue #1404718: Make "functions that call this" show hook_menu() callback usage of functions). [Strings and function calls were already turning into links in code.]
- On a hook page, you get links to separate pages for functions that implement the hook (as before - still done by basic pattern matching on function names *_hookname - but it's slightly better than before since I found out that _ means "match any character" in LIKE queries, so this eliminates some false matches), and now also to functions that invoke this hook via module_invoke, module_hook, drupal_alter, and several other functions. [Strings in e.g. module_invoke($module, 'hookname') were already turning into links in code.]
- On a theme function or theme template page, you get a link to a separate page showing the theme() calls to this function/template. [Strings in theme() were already turning into links to the theme function/template.]

Oh, and all of these reference list pages now also include tpl.php files as well as functions!

I also did some associated code refactoring:
- In api.pages.inc, there is now just one function that generates all of these "reference" pages, rather than the two that there used to be plus there would have been three more with this patch.
- In conjunction with that, I made one helper function to do the queries to count and list the actual referencing functions, to make sure they count and list are done the same way.
- I also made one helper function to generate the text that says "1 function calls zyz()", so that the page title on the references page, and the link to the references page, remain the same.

And fixed a couple of bugs I encountered:
- As mentioned above, hook implementations pages were listing things like... on the hook_cron page, it was showing functions called SomeClass::whateverDoesStuffWithCron, due to LIKE misunderstanding.
- References were not getting saved for tpl.php files, although that had been the intention. Now they are.
- References to hooks and string callback names were saved in the {api_reference_storage} table with '' around them, and now they're not.

And one other change that is kind of unrelated but it came up while testing this:
- On a non-function page, at the top where it shows you other versions of this file, constant, class, or whatever, the full file name (with path information) is shown rather than just basename(filename). The reason is that when I was looking at, for instance, page.tpl.php, I was seeing a sea of "8 page.tpl.php" with no information about which one was in modules/system, which one was bartik, etc. Having the full file name makes the section a bit more bulky, but a lot more useful.

And of course, some updates were made to the test samples and tests to verify that all of this stuff works. In particular, one of the sample theme templates was moved to a subdirectory, since during testing I found the theme template list of "who invokes this" wasn't working in subdirectories.

Phew! Anyway, this is now fixed!
http://drupalcode.org/project/api.git/commit/e9dae65

#13

Status:fixed» closed (fixed)

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

nobody click here