📜 ⬆️ ⬇️

Who works here or how to quickly find out information about callback for Drupal’s menu

Hi Habrayuzer!

I am a web developer, I program in PHP. My main focus is the development of sites using CMS / CMF Drupal. I work in a small IT organization, there are all sorts of projects (large, small, new, arriving at the end, etc.). Recently (in my case, this is already a year) I have come across such projects on which some have worked, then others, and still others ... And basically, on such projects, the customer asks, as a rule, to fix something on trivia (there is an element in the form to insert, there - an additional class is needed, etc.) But since large projects sometimes happen such that even it’s not always possible to determine “what is working here? which module? Since 2 years ago I learned about drush, it is very convenient, useful, but the most important thing is not to waste time rendering pages (it takes a lot to build a page). And then I realized that it would be good to somehow find out such petty information through the console, it turned out pretty quickly and conveniently, as it seems to me.

Most projects based on some kind of ready-made modular database (like Drupal) always have their own modules written exclusively for the goals of a specific project. Well, it is worthwhile to use one of them in our case for integration with drush. Let the module be called cm. Drupal 6 is used. Next, I provide the code from the cm.drush.inc file.

Standard hooks to describe the functionality of drush commands:
/** * Implementation of hook_drush_help(). */ function cm_drush_help($section) { switch ($section) { case 'drush:browse-url': return dt('Show URL\'s info'); case 'drush:browse-theme': return dt('Show theme function info'); } } /** * Implements hook_drush_command(). */ function cm_drush_command() { $items = array(); $items['browse-url'] = array( 'description' => dt('Show URL\'s info.'), 'arguments' => array( 'url' => dt('URL from browser'), ), 'aliases' => array('burl'), ); $items['browse-theme'] = array( 'description' => dt('Show theme function info.'), 'arguments' => array( 'theme_item' => dt('theme function'), ), 'aliases' => array('btheme'), ); return $items; } 

')

Now directly by the teams


browse-url (burl)

displays information about the callback that was passed to this command
 function drush_cm_browse_url($url) { $url = drupal_get_normal_path($url); drush_print(dt('Original path is: "!path"', array('!path' => $url))); $router_item = menu_get_item($url); if($router_item['file']) { require_once($router_item['file']); } $data = array(); $data['path'] = $router_item['path']; $data['access_callback'] = $router_item['access_callback']; $data['access_arguments'] = implode(", ", unserialize($router_item['access_arguments'])); $data['page_callback'] = $router_item['page_callback']; $data['page_arguments'] = implode(", ", unserialize($router_item['page_arguments'])); $data['tab_root'] = $router_item['tab_root']; $data['title'] = $router_item['title']; $data['title_callback'] = $router_item['title_callback']; drush_print(_cm_getting_function_info($router_item['page_callback'])); drush_print_table(drush_key_value_to_array_table($data)); } function _cm_getting_function_info($function_name) { include_once './includes/install.inc'; drupal_load_updates(); if (strpos($function_name, '::') === FALSE) { if (!function_exists($function_name)) { return drush_set_error(dt('Function not found')); } $reflect = new ReflectionFunction($function_name); } else { list($class, $method) = explode('::', $function_name); if (!method_exists($class, $method)) { return drush_set_error(dt('Method not found')); } $reflect = new ReflectionMethod($class, $method); } $func_info = array('!file' => $reflect->getFileName(), '!startline' => $reflect->getStartLine(), '!endline' => $reflect->getEndLine()); //drush_print_pipe(dt("!file -line !startline", $func_info)); return dt("Execute in : !file, lines !startline-!endline", $func_info); } 

The result of the work of the team is quite simpotichny
 $ drush burl admin Original path is: "admin" Execute in : /var/www/d6/modules/system/system.admin.inc, lines 11-59 path : admin access_callback : user_access access_arguments : access administration pages page_callback : system_main_admin_page page_arguments : tab_root : admin title : Administer title_callback : t 

we see the original path (this is in case if suddenly the system of aliases has already tried), in which file, which lines and the menu description itself. Pretty comfortable, isn't it? but the truth is, there are such callbacks and forms like here for example
 drush burl admin/settings/file-system Original path is: "admin/settings/file-system" Execute in : /var/www/d6/includes/form.inc, lines 70-149 path : admin/settings/file-system access_callback : user_access access_arguments : administer site configuration page_callback : drupal_get_form page_arguments : system_file_system_settings tab_root : admin/settings/file-system title : File system title_callback : t 

In this case, the script will be useful at least from the point of view that shows at least the name of the function for generating the form.

There is another command in the description of the hooks.
browse-theme (btheme)

 function drush_cm_browse_theme($theme_item) { init_theme(); $hooks = theme_get_registry(); var_dump($hooks[$theme_item]); } 

It shows information about the theme function, but the truth here is to make a reservation that it is made in draft form, so to speak, as is.
 $ drush btheme item_list array(7) { ["arguments"]=> array(4) { ["items"]=> array(0) { } ["title"]=> NULL ["type"]=> string(2) "ul" ["attributes"]=> NULL } ["type"]=> string(6) "module" ["theme path"]=> string(14) "modules/system" ["function"]=> string(15) "theme_item_list" ["include files"]=> array(0) { } ["theme paths"]=> array(1) { [0]=> string(14) "modules/system" } ["preprocess functions"]=> array(1) { [0]=> string(19) "template_preprocess" } } 

This is an example of displaying information about the theme function item_


In order to adapt all this to Drupal 7, it does not take much time (and maybe it will not be necessary at all, I did not check to be honest), I just came to finish these projects for me on the 6th version, so I built it for this version.

Source: https://habr.com/ru/post/145180/


All Articles