Created
May 2, 2013 23:29
-
-
Save vidrowan/5506231 to your computer and use it in GitHub Desktop.
Follow up to my blog post: Drupal – adding debug messages to your custom module – Improved - http://pages.uoregon.edu/vid/?p=2854
This is the code snippets I use to add debugging functionality to my custom Drupal modules.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Main form fields amendment: | |
| <? | |
| function mymodule_admin_settings($form, &$form_state) { //this is a funky comment | |
| #... | |
| $form = array_merge($form, _mymodule_debug_form()); //append settings form with debug fields | |
| return system_settings_form($form); | |
| }//end function mymodule_admin_settings($form, &$form_state) | |
| ?> | |
| <? | |
| /** | |
| * Page callback for admin/settings/mymodule. | |
| * | |
| * @see mymodule_menu() | |
| */ | |
| function mymodule_admin_settings($form, &$form_state) { | |
| mymodule_debug_log('Running function mymodule_admin_settings'); | |
| // Checkbox to enable or disable the tab. | |
| $form['mymodule_your_settings'] = array( | |
| '#type' => 'checkbox', | |
| '#default_value' => variable_get('mymodule_enabled', FALSE), | |
| '#title' => t('Enable tab'), | |
| '#description' => t('Activate the tab.'), | |
| ); | |
| //include the debug form fields | |
| $form = array_merge($form, _mymodule_debug_form()); | |
| return system_settings_form($form); | |
| } | |
| ?> | |
| debug form fields that get included above: | |
| <? | |
| /** | |
| * Helper to produce mymodule debug configuration. | |
| * | |
| */ | |
| function _mymodule_debug_form() { | |
| mymodule_debug_log('Running function _mymodule_debug_form'); | |
| // Debugging - fieldset for debug related fields | |
| $form['mymodule_debug'] = array( | |
| '#type' => 'fieldset', | |
| '#title' => t('Debugging'), | |
| '#collapsible' => TRUE, | |
| '#collapsed' => FALSE, | |
| ); | |
| $form['mymodule_debug']['mymodule_debug_message'] = array( | |
| '#type' => 'checkbox', | |
| '#title' => t('Enable debugging message feedback'), | |
| '#description' => t('Displays feedback in Drupal Messages'), | |
| '#default_value' => variable_get('mymodule_debug_message', FALSE), | |
| '#required' => FALSE | |
| ); | |
| $form['mymodule_debug']['mymodule_debug_reporting'] = array( | |
| '#type' => 'checkbox', | |
| '#title' => t('Enable watchdog debugging feedback'), | |
| '#description' => t('Displays feedback messages in the Recent Log Entries (watchdog) Report'), | |
| '#default_value' => variable_get('mymodule_debug_reporting', FALSE), | |
| '#required' => FALSE | |
| ); | |
| return $form; | |
| } //function _mymodule_debug_form() | |
| ?> | |
| The actual debug log function | |
| <? | |
| /** | |
| * Helper to output debug messages. | |
| * | |
| * mymodule_debug_log([$errMsgArr(array), $errLabel(string)='mymodule', $errSeverity(string)='status|warning|error', $force(bool)=FALSE]) | |
| * | |
| */ | |
| function mymodule_debug_log($errMsgArr='', $errLabel='mymodule', $errSeverity='status', $force=FALSE) { | |
| $backtrace = debug_backtrace(); | |
| $watchdog_levels=watchdog_severity_levels(); | |
| if ( ! is_array($errMsgArr)){ | |
| $errMsgArr = array($errMsgArr); | |
| }// end if ( ! is_array($errMsgArr) ) | |
| foreach ( $errMsgArr as $errMsg ) { | |
| $spacer = ($errMsg)?' :: ':''; | |
| $errMsg = $errMsg . $spacer .'Line: ' . $backtrace[0]["line"] . '. Function: ' . $backtrace[1]["function"] . '. File: ' . array_pop(explode("/",$backtrace[0]["file"])); | |
| if ( variable_get('uoasymptote_debug_message', FALSE) || $force ){ | |
| drupal_set_message(t($errMsg), $errSeverity); | |
| }// end if | |
| if ( variable_get('uoasymptote_debug_reporting', FALSE) || $force ) { | |
| #watchdog($errLabel,$errMsgArr); | |
| watchdog($errLabel,$errMsg,array(),array_search($errSeverity,$watchdog_levels)); | |
| }// end if | |
| }// end loop | |
| unset($backtrace); | |
| }// function _uoasymptote_debug_form() | |
| ?> | |
| Example usage in the form: | |
| <? | |
| function mymodule_form_validate($form, &$form_state) { | |
| mymodule_debug_log(); //outputs: Line: 109. Function: mymodule_form_validate. File: mymodule.module | |
| ... | |
| } | |
| ... | |
| if ( ! $path ) { | |
| //force this debug message to the screen | |
| mymodule_debug_log('Notice: '. mymodule_library_location_stmt(), 'mymodule', 'error', TRUE); | |
| } else { | |
| ... | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment