Advertisement
quang4dev

Show table in Drupal

Apr 2nd, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.49 KB | None | 0 0
  1. function vnfish_menu() {
  2. $items['records'] = array(
  3.         'title' => "Record list",
  4.         'page callback' => 'drupal_get_form',
  5.         'page arguments' => array('record_list_form'),
  6.         'access arguments' => array('access content'),
  7.         'type' => MENU_NORMAL_ITEM,
  8.         'file' => "model/vnfish.model.inc",
  9.     );
  10.  
  11.     return $items;
  12. }
  13.  
  14. /**
  15.  * Show list of record
  16.  * @return theme
  17.  */
  18. function record_list_form() {
  19.     drupal_add_css(drupal_get_path('module', 'vnfish') . '/stylecss.css', array('group' => CSS_DEFAULT, 'type' => 'file'));
  20.  
  21.     // Set header
  22.     $header = array(
  23.         array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
  24.         array('data' => t('Summery'), 'field' => 'summery'),
  25.         array('data' => t('Content'), 'field' => 'content'),
  26.         array('data' => t('Status'), 'field' => 'status'),
  27.         array('data' => t('Created / Modified'), 'field' => 'created'),
  28.         array('data' => t('Action')),
  29.     );
  30.     $header = array(
  31.         'title' => array('data' => t('Title'), 'field' => 'title'),
  32.         'summery' => array('data' => t('Summery'), 'field' => 'summery'),
  33.         'content' => array('data' => t('Content'), 'field' => 'content'),
  34.         'status' => array('data' => t('Status'), 'field' => 'status'),
  35.         'status' => array('data' => t('Status'), 'field' => 'status'),
  36.         'created' => array('data' => t('Created / Modified'), 'field' => 'created'),
  37.         'operations' => array('data' => t('Operations')),
  38.     );
  39. //    , 'class' => $node->status ? 'published' : 'unpublished'
  40.     // Next create Sql query to be executed which returns the sorted and paged results from the database
  41.     $query = db_select('vnfish', 'a')
  42. //    ->condition('a.status', 1) //Only published nodes, change condition as it suits you
  43.     ->extend('PagerDefault')  //Pager Extender
  44.     ->limit(5)    //5 results per page
  45.     ->extend('TableSort')  //Sorting Extender
  46.     ->orderByHeader($header)//Field to sort on is picked from $header
  47.     ->fields('a', array(
  48.         'id',
  49.         'title',
  50.         'summery',
  51.         'content',
  52.         'status',
  53.         'created',
  54.         'modified',
  55.     ));
  56.  
  57.     // The next step is executing the query and collecting the rows from the resultset.
  58.     $results = $query->execute();
  59.  
  60.     $rows = array();
  61.     $i = 0;
  62.     foreach ($results as $k => $node) {
  63.         $rows[$node->id] = array(
  64.             'title' => l($node->title, 'records/' . $node->id . '/edit'),
  65.             'summery' => t($node->summery),
  66.             'content' => t($node->content),
  67.             'status' => array('data' => ' ', 'class' => $node->status ? 'published' : 'unpublished'),
  68.             'created' => array(
  69.                 'data' => $node->modified ? format_date($node->modified) : ($node->created ? format_date($node->created) : "___, __/__/____ - __:__"),
  70.                 'style' => ($node->modified || $node->created) ? "" : "color: silver;"
  71.             ),
  72.         );
  73.         $destination = drupal_get_destination();
  74.         // Build a list of all the accessible operations for the current node.
  75.         $operations = array();
  76.         $operations['edit'] = array(
  77.             'title' => t('edit'),
  78.             'href' => 'records/' . $node->id . '/edit',
  79.             // 'query' => $destination,
  80.         );
  81.         $operations['delete'] = array(
  82.             'title' => t('delete'),
  83.             'href' => 'records/' . $node->id . '/delete',
  84.             // 'query' => $destination,
  85.         );
  86.         $rows[$node->id]['operations'] = array();
  87.         if (count($operations) > 1) {
  88.             // Render an unordered list of operations links.
  89.             $rows[$node->id]['operations'] = array(
  90.                 'data' => array(
  91.                     '#theme' => 'links__node_operations',
  92.                     '#links' => $operations,
  93.                     '#attributes' => array('class' => array('links', 'inline')),
  94.                 ),
  95.             );
  96.         } elseif (!empty($operations)) {
  97.             // Render the first and only operation as a link.
  98.             $link = reset($operations);
  99.             $rows[$node->id]['operations'] = array(
  100.                 'data' => array(
  101.                     '#type' => 'link',
  102.                     '#title' => $link['title'],
  103.                     '#href' => $link['href'],
  104.                     '#options' => array('query' => $link['query']),
  105.                 ),
  106.             );
  107.         }
  108.     }
  109.  
  110.     $admin_access = user_access('administer nodes');
  111.     $form['options'] = array(
  112.         '#type' => 'fieldset',
  113.         '#title' => t('Action options'),
  114.         '#attributes' => array('class' => array('container-inline')),
  115.         '#access' => $admin_access,
  116.     );
  117.     $options = array();
  118.     $options['vnfish_record_publish'] = "Publish";
  119.     $options['vnfish_record_unpublish'] = "Unpublish";
  120.     $options['vnfish_record_remove'] = "Remove";
  121.     $form['options']['operation'] = array(
  122.         '#type' => 'select',
  123.         '#title' => t('Operation'),
  124.         '#title_display' => 'invisible',
  125.         '#options' => $options,
  126.         '#default_value' => 'approve',
  127.     );
  128.     $form['options']['submit'] = array(
  129.         '#type' => 'submit',
  130.         '#value' => t('Go'),
  131.         '#submit' => array('vnfish_record_update'),
  132.     );
  133.     $form['options']['add'] = array(
  134.         '#type' => 'submit',
  135.         '#value' => t('Add'),
  136.         '#submit' => array('vnfish_form_record_add'),
  137.     );
  138.     $form['ids'] = array(
  139.         '#type' => 'tableselect',
  140.         '#header' => $header,
  141.         '#options' => $rows,
  142.         '#empty' => t('No content available.'),
  143.     );
  144.     $form['pager'] = array('#markup' => theme('pager'));
  145.  
  146. //    $table_attributes = array('id' => 'my-table');
  147. //    
  148. //    // Then, we create a table from the headers and the result rows with a simple call to theme (or theme_table methods):
  149. //    $html = theme('table', array(
  150. //        'header' => $header,
  151. //        'rows' => $rows,
  152. //        'caption' => 'My Table', //Optional Caption for the table
  153. //        'sticky' => TRUE, //Optional to indicate whether the table headers should be sticky
  154. //        'empty' => 'No record created...', //Optional empty text for the table if resultset is empty
  155. //        'attributes' => $table_attributes, //Separating Rows
  156. //    )
  157. //    );
  158. //
  159. //    // The final step is to append a pager to the table.
  160. //    $html .= theme('pager', array(
  161. //        'tags' => array()
  162. //    )
  163. //    );
  164. //    $html .= drupal_render(drupal_get_form("vnfish_form_record_add"));
  165.  
  166.     return $form;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement