diff -u b/dbtng_example/dbtng_example.module b/dbtng_example/dbtng_example.module --- b/dbtng_example/dbtng_example.module +++ b/dbtng_example/dbtng_example.module @@ -346,7 +346,7 @@ break; case 'examples/dbtng/grouping_list': - $output = t('Generate a list of all entries in the database in a group. The records will be displayed in grouping format for column "age".'); + $output = t('Groups the result set by the specified field and render a list of entries in the database. The records will be displayed in grouping format for column "age".'); break; } return $output; @@ -395,7 +395,7 @@ $items['examples/dbtng/grouping_list'] = array( 'title' => 'Grouping list', 'page callback' => 'dbtng_example_grouping_list', - 'access arguments' => array('access dbtng example grouping list'), + 'access arguments' => array('access administration pages'), 'type' => MENU_LOCAL_TASK, ); @@ -403,22 +403,6 @@ } /** - * Implements hook_permission(). - * - * This hook can supply permissions that the module defines, so that they can - * be selected on the user permissions page and used to grant or restrict - * access to actions the module performs. - */ -function dbtng_example_permission() { - return array( - 'access dbtng example grouping list' => array( - 'title' => t('Permission to view Grouping List'), - 'description' => t('Give permission to access Grouping List.'), - ), - ); -} - -/** * Render a list of entries in the database. */ function dbtng_example_list() { @@ -604,37 +588,51 @@ /** * This function groups the result set by the specified field and render a * list of entries in the database - * + */ +function dbtng_example_grouping_list() { + $result = dbtng_example_execute_group_by_select_query(); + return dbtng_example_render_resultset_as_table($result); +} + +/** + * The code below will result in the following query * SELECT ex.pid AS pid, ex.uid AS uid, ex.name AS name, ex.surname AS surname, * ex.age AS age FROM {dbtng_example} ex GROUP BY ex.age - * */ -function dbtng_example_grouping_list() { +function dbtng_example_execute_group_by_select_query() { $dbtng_example_execute_group_by_select_query = db_select('dbtng_example', 'ex')->fields('ex'); // 'ex.age' is the field on which to group $dbtng_example_execute_group_by_select_query->groupBy('ex.age'); // generate the result in object format. $dbtng_example_result = $dbtng_example_execute_group_by_select_query->execute()->fetchAll(); - $output = ''; - if ($dbtng_example_result) { - foreach ($dbtng_example_result as $row) { + return $dbtng_example_result; +} + +/** + * This function renders a resultset as table + */ +function dbtng_example_render_resultset_as_table($result) { + $rows = array(); + if ($result) { + foreach ($result as $row) { // Sanitize the data before handing it off to the theme layer. $rows[] = array_map('check_plain', (array) $row); } - $output .= dbtng_example_render_resultset_as_table($rows); - } - else { - drupal_set_message(t('No records found.')); } - return $output; + return dbtng_example_convert_resultset_to_table_render_array($rows); } -function dbtng_example_render_resultset_as_table($rows = array()) { - // Make a table. +/** + * This function renders array for table 'dbtng_example' + */ +function dbtng_example_convert_resultset_to_table_render_array($rows = array()) { $header = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age')); - $output = theme('table', array('header' => $header, 'rows' => $rows)); - - return $output; + return [ + '#theme' => 'table', + '#header' => $header, + '#rows' => $rows, + '#empty' => t('No records found'), + ]; } /** * @} End of "defgroup dbtng_example".