0

I have created a custom page which has a db query to return content, which is using the pagerdefault extender on a Drupal 7 install.

However when I return the content the pager shows but not the content. This is the code after the query:

$content = array();
foreach ($result AS $events)
{
    $content[] = '<div class=\'event\'>' . $events->type . ' - ' . $events->nid . ' - ' . $events->tutorial_start . $events->event_start . '</div>';
}
$build += $content;
$build['pager'] = array(
    '#theme' => 'pager',
    '#weight' => 1,
);
return $build;

I can see the $content rows if I dsm $build, but not display them on the page. How do I render them on the page?

0

There are two things you should know to make it works:

  1. Your query should extend PagerDefault.
  2. Element inside content should be Rendered Array

here is a working example adjust the code to your needs:

//- Build array
  $build = array();
  /**
   * @TODO: adjust the query to your needs.
   */
  //- Prepare the Query to Extend PagerDefault
  $query = db_select('node', 'n')->extend('PagerDefault');
  $nids = $query
    ->fields('n', array('nid'))
    ->condition('type', 'page')
    ->condition('status', 1)
    ->execute()
    ->fetchCol();
  if (!empty($nids)) {
    $nodes = node_load_multiple($nids);
    $content = array();
    foreach ($nodes as $event) {
      //- Prepare the content
      $content[] = array(
        '#type'   => 'markup',
        '#markup' => '<div class="event">' . $event->type . ' - ' . $event->nid . '</div>',
      );
    }
    //- Add Content to the build
    $build += $content;
    $build['pager'] = array(
      '#theme'  => 'pager',
      '#weight' => 5,
    );
  }
  return $build;
  • Ah thank you, that makes sense. I was looking at the blog display code where it uses $build+= node_view_multiple() which must be already formatted in a render array. – lumin1907 Oct 27 '18 at 10:26

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.