1

I have an entity called announcement, and I have a page (user/%/announcement) where I display a table of three columns (name of entity,an edit entity link and an delete entity link).

function announcements_user($account) {
  $uid = $account->uid;
  $return = '';
  $total = 0;
  $i=0;
  $pid = array();
  $text = array();

  $query = db_select('announcements','a')->extend('PagerDefault');
  $query->fields('a',array('pid','value'));
  $query->condition('uid', $uid);
  $result = $query->execute();

  foreach($result as $row) {
    $pid[] = $row->pid;
    $text[] = $row->value;  
    $total++;
  }

  $return .= '<table border="1">
    <tr>
     <th>Announcement</th>
     <th>Edit</th>
     <th>Delete</th>
    </tr>';

  while ($total) {
    $return .= '<tr>';
    $return .= '<td><a href="'.$base_url.'/announcement/'.$pid[$i].'">'.substr($text[$i],0,10).'......</a></td>';
    $return .= '<td><a href="'.$base_url.'/announcement/edit/'.$pid[$i].'">Edit</a></td>';
    $return .= '<td><a href="'.$base_url.'/announcement/delete/'.$pid[$i].'">Delete</a></td>';
    $return .= '</tr>';
    $i++;
    $total--;
  } 

  $return .='</table>';
  $return .='<br>';
  $return .='<a href ="'.$base_url.'/announcement/add">Add Announcement</a>';

  return $return;   
}

Now my problem is that keeping this format of tabular structure, how do I display my result in a paginated format with 5 announcements per page? I have seen pager.inc for Drupal 7 and howto-print-out-the-pagination-for-entityfieldqueries link, but I just can't figure out how to apply them to my case.

  • If you are using the PagerDefault extender, you don't need to create the HTML for the table yourself. – kiamlaluno May 7 '11 at 17:22
  • so how do i get the edit and delete links then? – ayush May 7 '11 at 17:34
3
function announcements_user($account) {
  $header = array(
    'announcement' => array(t('Announcement')),
    'operations' => array('data' => t('Operations'), 'colspan' => '2'),
  );
  $rows = array();

  $result = db_select('announcements','a')
    ->fields('a', array('pid','value'))
    ->condition('uid', $account->uid)
    ->extend('PagerDefault')
    ->limit(5)
    ->execute();

  foreach ($result as $data) {
    $row = array();
    $row[] = l(truncate_utf8($data->value, 10, FALSE, TRUE), 'announcement/' . $data->pid);
    $row[] = l(t('Edit'), 'announcement/edit/' . $data->pid);
    $row[] = l(t('Delete'), 'announcement/delete/' . $data->pid);

    $rows[] = $row;
  }

  $form['announcements'] = array(
    '#theme' => 'table', 
    '#header' => $header, 
    '#rows' => $rows, 
    '#empty' => t('No announcement available.'),
  );

  $form['pager'] = array('#markup' => theme('pager'));
  return $form;
}
  • Query methods generally return the object for which they are invoked; this means that method calls can be concatenated as I did in the code.
  • PagerDefault::limit() has a default limit of 10; if you don't call that method, the limit is the default value.
  • Strings that appear in the user interface should always be translated; the only strings that cannot be translated (which means, you cannot pass them to t()) are dynamic strings obtained from a function/method.
  • Drupal URLs should always be obtained from url() or l(). l() returns the HTML tag for links (<a>), while url() returns the relative or absolute URL for the Drupal path passed as argument.

For an example of function that does something similar, see blog_page_last(), which returns the last blog posted by a user.

  • I was working on pretty much the same thing, you were faster :) – Berdir May 7 '11 at 19:22
  • absolutely great..awesome..i will look into the code how it worked but superb stuff..thanks a lot:) – ayush May 7 '11 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.