background image

Content tagged with: node type

Eric's picture

A rotating image block can easily be created by using modules that you make already have installed. If you have not already download, install, and enable the following modules:

Views
CCK
FileField
ImageField
ImageAPI
ImageCache

After you've installed these modules, you'll have to adjust the user permissions (admin/user/permissions) to ensure the correct roles have permission to view and administer these modules.

Create a new ImageCache preset (admin/build/imagecache). In my example, I named it "home_rotating". I also added a Scale action to ensure all the images are the same width. It's probably a good thing to check the region width in your theme and adjust the image width accordingly.

Define a new content type (admin/content/types/add). In my example, I named it "image_home_rotating". I left off the body field label because I am only concerned with displaying the image (no other text). Add a new field to the new content type, and choose File for the Field type and Image for the Widget. I named mine "field_image_home_rotating" and labeled it "Image". After adding the field, click on the Display Fields tab. For teaser and Full node, choose the ImageField preset you just created. This will ensure the right image size is used when viewing the node.

Create a new view (admin/build/views/add). Make sure you choose Node for View Type on the first form screen. Add two filters: one for published nodes; and one for node type, and choose the newly created node type. To get a random selection of nodes, add Sort Criteria and choose "Global: Random". Limit the Items to Display to 1. You can leave the Style set to Unformatted and the Row style to Fields. Add a new field and select the newly created image field. When adding the field, the Format setting allows you to change the ImageField preset. If it's not already set, choose your new preset. I also choose to hide the Field label on this step. Add the Block display to the view and save it.

Lastly, assign this block view to the region in your theme and add a few image nodes. As you refresh the page, the image should change.

Eric's picture

I recently created a block view that I wanted to include only on pages of a certain node type. I decided to create a PHP function that returns true or false based based on the page URL and then call the function via the "Page specific visibility settings", on the block edit screen.

This function returns true or false based on the what type of node the page URL is. I added this function to my template.php file, but you could add it to a module if desired.

<?php
function _MYTHEME_MYBLOCK_visibility() {
 
// look up the system path from the page URL
 
$path = drupal_lookup_path('source', $_REQUEST['q']);

 
// check if the page is a node
 
if (substr($path,0,5)=='node/') {
   
// load node
   
$node = node_load(substr($path,5));
       
   
// check if node exists
   
if (!$node) return false;
       
   
// check node type
   
if ($node->type == 'MYNODETYPE') return true;
       
  } else {
    return
false;   
  }
}
?>

Then I added a PHP snippet in the "Page specific visibility settings" on the block edit screen:

<?php
if (function_exists('_MYTHEME_MYBLOCK_visibility')) return _MYTHEME_MYBLOCK_visibility();
return
false;
?>

Now, the block only shows on pages of "MYNODETYPE".

Eric's picture

This code snippet will group your search results in collapsible fieldsets by node type.

<?php
function MYTHEME_search_page($results, $type) {
 
$html = '<dl class="search-results">';

 
// get a list of node types
 
$nodeTypes = node_get_types();
   
 
// loop through results, group by type
 
$resultTypes = array();
  foreach (
$results as $result) {
   
$resultTypes[$result['node']->type][] = $result;   
  }
   
 
// create fieldsets for each type
 
foreach ($resultTypes as $resultType => $resultTypeResults) {
   
$value = "";
   
// loop through entries
   
foreach ($resultTypeResults as $entry) {
     
$value .= theme('search_item', $entry, $type);
    }
       
   
// add fieldset
   
$html .= theme('fieldset',
      array(
       
'#title' => $nodeTypes[$resultType]->name,
       
'#collapsible' => TRUE,
       
'#collapsed' => FALSE,
       
'#value' => $value,
      )
    );
       
  }
   
 
$html .= '</dl>';
 
$html .= theme('pager', NULL, 10, 0);
   
  return
$html;
}
?>