-1

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.

ndrizza
  • 3,285
  • 6
  • 27
  • 41

1 Answers1

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

Vlad Stratulat
  • 1,296
  • 1
  • 10
  • 24
  • yes, I've seen this on the drupal doc. but i would like not to be constrained by a specific view like [ theme('item_list', array('items' => $items)); ] how can i output plain dynamically generated html? – ndrizza Sep 27 '12 at 10:05
  • What do you mean by "plain dynamically generated html"? – Vlad Stratulat Sep 27 '12 at 10:30
  • i'd like to do something like echo "
    Hello World + some result
    " and have this displayed in the block
    – ndrizza Sep 27 '12 at 12:38