Skip to main content
fixed a typo ("string" versus "strings")
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
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 stringstrings 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.

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 string 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.

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.

Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

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 string 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.