Jonny C
Jonny C

Reputation: 1941

Whats the correct way of outputting html in a custom module in Drupal

I have worked on a few project with drupal, and the main suggestion I got when learning was to use Views to output your data, although this was fine for some items I found it cumbersome to achieve my projects requirements in the set time and to style it the controls were too limiting.

I want to know whats wrong with doing it like this, or whats right about doing it like this?

You run a query to get the specific data you want.

function custom_block_get_item($nid){
  $result = db_query("SELECT `field_custom_item`  FROM {field_data_field_custom_item} WHERE entity_id = :entity_id", 
  array(
    ':entity_id' => $nid,
  ));
return $result;
}

Then you loop through creating the html

function custom_block_display_blurb($nid){
$html='<div id="blurb_wrapper"><div id="recent_projects_blurb">';
  $result=custom_block_get_blurb($nid);
    while($row1 = $result->fetchAssoc()){
  $a=check_markup($row1['field_custom_item'],'full_html','','');
  $html.='
  <span class="recent_project_text">'.
  $a.'</span>';
 } 
$html.='</div></div>';
return $html;
}

Finally display it at the end

function custom_block_block_view($delta=''){
  if (arg(0) == 'node' && is_numeric(arg(1))) $nodeid = arg(1);
    switch($delta) {
     case 'custom_block':
   $block['content'] = custom_block_display_blurb($nodeid);  
  break;
  }
  return $block;
}

This seems like a kind of hacky way to do things, what is the correct drupal way?

Upvotes: 1

Views: 41

Answers (1)

2pha
2pha

Reputation: 10155

Maybe you mean using the drupal theme layer? This allows for use of .tpl files and overriding. I have a simple block module that does it here Source here.

Here is some documentation on using the theme layer within a module.

Here is some official Drupal docs on themeing (mainly themes rather than using the theme layer in a module).

Upvotes: 1

Related Questions