Search code examples
htmldrupaldynamicviewgenerated

drupal custom block module: how to display dynamic generated html from hook_block_view(...)?


i would like to output dynamic generated html content instead of the translatable message but i can't make it work:

function custom_logo_module_block_view($delta = '') {

    // don't worry about switch($delta) logic

    // perform some operations and then display some generated html

    // (maybe use the template(...) function)

    // works fine but i'd like to print html
    $block['content'] = t('No content available.');

    return $block;
}

how can i print out generated html into a block?

i can't find any solutions or code examples. i think i might be pointing towards the wrong direction so best practice suggestions are welcome.


Solution

  • function custom_logo_module_block_view($delta = '') {
      $block = array();
      if ($delta == 'example') {
        $block = array(
          'subject' => t('Active users list'),
          'content' => example_block_content()
        );
      }
      return $block;
    }
    
    function example_block_content() {
      // Query for active users from database
      $users = db_select('users', 'u')
        ->fields('u', array('uid', 'name'))
        ->condition('u.status', 1)
        ->execute()
        ->fetchAll();
    
      // Prepare items for item list
      $items = array();
      foreach ($users as $user) {
        $items[] = l($user->name, "user/{$user->uid}");
      }
    
      $output = t('No active users available.');
    
      if (!empty($items)) {
        $output = theme('item_list', array('items' => $items));
      }
    
      return $output;
    }
    

    Update regarding your comments...

    As far as I understand by some result you mean generated data from database. In this case you can try something like this:

    function example_block_content() {
      // Query for active users from database
      $users = db_select('users', 'u')
        ->fields('u', array('uid', 'name'))
        ->condition('u.status', 1)
        ->execute()
        ->fetchAll();
    
      $output = '';
      foreach ($users as $user) {
        $output.= '<div>'. $user->name .'</div>';
      }
    
      $output = "<div>Hello World". $output ."</div>";
    
      return $output;
    }
    

    This will give you the following output:

    <div>Hello World
      <div>admin</div>
      <div>ndrizza</div>
      <div>Vlad Stratulat</div>
      ...
    </div>
    

    Or you can try:

    function custom_logo_module_block_view($delta = '') {
      $block = array();
      if ($delta == 'example') {
        $block = array(
          'subject' => t('Active users list'),
          // this will return "Hello World + some result" text inside <div>
          'content' => "<div>Hello World + some result</div>"
        );
      }
      return $block; 
    }
    

    Both of this ways are working but they are not the right ways. The right way to generate content is in my first answer. Read more about theming in Drupal.