drupaldrupal-7drupal-modulesdrupal-hooks

Drupal 7: custom module: display custom contentTypy in custom block


I'm new at Drupal 7, so I have a question.

I have my own content type Writers that includes such fields as Title, Years of life, Photo, Description.

I have a task to display 3 random Writers at page. Actual I've done it with help of Views module, but I want to do it myself.

So I created my own module random_content like that:

<?php

function random_content_help($path, $arg) {
  switch ($path) {
    case "admin/help#random_content":
    return '<p>'. t("Displays random content") .'</p>';
   break;
  }  
}

function random_content_block_info() {
  $blocks['random_content'] = array(
    'info' => t('Random content'), 
    'cache' => DRUPAL_CACHE_PER_ROLE, 
  );
  return $blocks;
}

function random_content_contents() {
    $query = db_select('node', 'n')
    ->fields('n', array('nid', 'title'))
    ->condition('type', 'writers')
    ->orderBy('rand()') 
    ->range(0,3)
    ->execute();
    return $query; 
}

function random_content_block_view($delta = '') {
  switch($delta){
    case 'random_content':
    $block['subject'] = t('Random content');
    if(user_access('access content')) {
      $result = random_content_contents();       
      $items = array();           
      foreach ($result as $node){
                $items[] = array(
                'data' => l($node->title, 'node/' . $node->nid) . '</br>',
              );
      }
      if (empty($items)) { 
        $block['content'] = t('No data availible.'); 
      } else {
        $block['content'] = theme('item_list', array(
          'items' => $items));
      }
    }
  }
  return $block;
 }

As you can see, I've learned only to add links to particular content. But how can I display full information including Title, Years of life, Photo and Description?


Solution

  • To display the full node, or parts of it you need to load the node. E.g.

    $my_node = node_load($nid);
    
    $render_array = array();
    $render_array['title'] = array(
       '#type' => 'markup',
       '#markup' => $my_node->title
    );
    $author = field_get_items('node', $my_node, 'field_author','und');  
    $render_array['author'] = array(
       '#type' => 'markup',
       '#markup' => $author[0]['safe_value']
    );
    
    // or as some like to do it
    
    $render_array['author'] = array(
       '#type' => 'markup',
       '#markup' => $my_node->field_author['und'][0]['value']
    );
    echo drupal_render($render_array);
    

    Note the 'und' constant means the language is undefined. If you have translation/language enabled and different content for different languages you would have to use 'en', 'de' etc. for the appropriate language.

    You can also let drupal render the node and then manipulate or retrieve individual items. Like this

    $my_node = node_load($nid);
    $build = node_view($my_node,'full');
    $build['body'][0]['#markup'] = $build['body'][0]['#markup'].' some addition';
    $build['field_author'][0]['#markup'] = $build['field_author'][0]['#markup'].' my favorite';
    
    echo drupal_render($build);
    

    The advantage of using this latter method is that then the whole themeing engine kicks in, and all hooks that are set to act on the content etc. Of course, if you only want to retrieve values you don't need that.

    Note also, I assume your author field is named field_author. You should check that in the field edit window for the content type.