0

I am learning how to write modules at the moment. The following piece of code simply pulls out of the database some data and selects just the nid, title and created fields

function photo_gallery_contents($display){

$max_num = variable_get('photo_gallery_max', 3);

$query = db_select('node', 'n')
->fields('n', array('nid', 'title', 'created'))
->condition('status', 1) //Published.    
->condition('type','portfolio') // Only want our photos aka Portfolio content type
->orderBy('created', 'DESC'); //Most recent first.

if ($display == 'block') {
// Restrict the range if called with 'block' argument
$query->range(0, $max_num);
} // Now proceeds to execute().
// If called by pge, query proceeds directly to execute()

return $query->execute();

}

This works fine but how do I access the other fields I created for my "portfolio" content type such as "field_image" or "field_category" both of which I added? I just cannot work out how to do this. Your help will be so appreciated!

I've written other hooks that represent this information on the page or block, at the moment I am showing a list of links to my nodes. I want them to be images with links instead:

function _photo_gallery_page() {
$result = photo_gallery_contents('page');
//Array to contain items for the page to render.
$items = array();
//Iterate over the resultset and format as links.
foreach ($result as $node) {
  $items[] = array(
  'data' => l($node->title, 'node/' . $node->nid),
  ); 
}

if (empty($items)) { //No content in the last 2 weeks.
  $page_array['photo_gallery_arguments'] = array(
    //Title serves as page subtitle
    '#title' => t('All photos from ever'),
    '#markup' => t('No photos available.'),
  );
  return $page_array;  
} 

 else {
  $page_array['photo_gallery_arguments'] = array(
    '#title' => t('All photos from ever'),
    '#items' => $items,
    //Theme hook with suggestion.  
    '#theme' => 'item_list__photo_gallery',
  );
  return $page_array;
}

}

So how do I swap $node->title to show the image instead?

2

The best way would be to grab just the node ids from the table, and use the node_load_multiple() function to load the node objects.

The field data will be attached to those objects, and you can use other API functions to extract the data/build the display HTML (field_get_items() and field_view_field() respectively).

Some sample code that should help:

function photo_gallery_contents($display){
  $max_num = variable_get('photo_gallery_max', 3);

  $query = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->condition('status', 1) //Published.    
    ->condition('type','portfolio') // Only want our photos aka Portfolio content type
    ->orderBy('created', 'DESC'); //Most recent first.

  if ($display == 'block') {
    // Restrict the range if called with 'block' argument
    $query->range(0, $max_num);
  }

  // Fetch the results for the nid field into an array
  $nids = $query->execute()->fetchCol(); 

  // Load the node objects and return
  return node_load_multiple($nids);
}

function _photo_gallery_page() {
  $result = photo_gallery_contents('page');

  //Array to contain items for the page to render.
  $items = array();
  //Iterate over the resultset and format as links.
  foreach ($result as $node) {
    // Build a render array based on the field's display settings.
    $image = field_view_field('node', $node, 'field_image');

    // Render the array down to the image HTML
    $image_html = drupal_render($image);

    $items[] = array(
      'data' => l($image_html, 'node/' . $node->nid, array('html' => TRUE)),
    ); 
  }
}

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.