Last active
December 18, 2015 09:49
-
-
Save jonhattan/5764326 to your computer and use it in GitHub Desktop.
Example of building a table with rows from a db table in Drupal
This file contains 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
<?php | |
/** | |
* Menu callback. | |
*/ | |
function this_is_a_menu_callback() { | |
$header = array('nid' => 'ID', 'title' => 'Title'); | |
$query = db_select('node', 'n', array('fetch' => PDO::FETCH_ASSOC)) | |
->fields('n', array('nid', 'title')) | |
->extend('PagerDefault') | |
->limit(10) | |
->extend('TableSort') | |
->orderByHeader($header); | |
$rows =$query | |
->execute() | |
->fetchAllAssoc('nid'); | |
$build = array(); | |
$build['nodes'] = array( | |
'#theme' => 'table', | |
'#header' => $header, | |
'#rows' => $rows, | |
'#empty' => t('No nodes available.'), | |
); | |
$build['pager'] = array( | |
'#theme' => 'pager', | |
); | |
//print render($build); | |
return $build; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment