Drupal Dateianhang per Block in Sidebar

Sa, 07.11.2009 - 14:29 -- Daniel Espendiller

In Drupal können an einen Eintrag (node) Datei und Bilder gehangen werden. Diese können in einen eigenen Block ausgeben werden, welcher einfacher positioniert werden kann. Zusätzlich können mit einem Zusatzmodul Weblinks an einen Beitrage "geheftet" werden.
Alle Einträge werden normalerweise unter den Beitrag gelegt. Somit überließt man diese schnell, deswegen die Auslagerungen in einen Block.
Im Block werden alle Dateianhänge aufgelistet und alle Weblinks, die "Download" oder "Source" in der Beschreibung stehen haben. Einfügt werden muss dieser Quelltext direkt in Blockcontent als PHP-Eintrag (eventuell das Coremodul PHP-Filter aktivieren).

Download Block

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $node = node_load(arg(1));
  $rows = array();
 
if (count($node->links_related)) {
  foreach($node->links_related as $r_link) {
if ( strpos(strtolower("a".$r_link['link_title']),"download")>0 OR strpos(strtolower("a".$r_link['link_title']),"source")>0 ) {
        $rows[] = array(l("Download", $r_link['url']),$r_link['link_title'], "extern", "");
}
 
  }
}
 
  $files = upload_load($node);
  foreach ($files as $file) {
    if ($file->list) {
      $href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
      $text = $file->description ? $file->description : $file->filename;
      $mime = strtoupper(substr($file->filemime, strrpos($file->filemime, '/')+1));
      $rows[] = array(l("Download", $href),$text, $mime.", ".format_size($file->filesize));
    }
  }
if (count($rows)>0) {
  foreach($rows as $row){
   $listBullets .= '<li>'.$row[0].'<br/><small>'.$row[1].' ['.$row[2].']</small>'.'</li>';
  }
 
   print '<ul>'.$listBullets.'</ul>';
}
 
}

Externen Quellen als Vorlage genommen, falls der orignale Quelltext benötigt wird:

Attached images in a block

This snippet displays attached images (which requires the image module and image_attach) in a block. I'm no PHP expert, but I managed to combine the general block loading code (lines 1 & 2) with the theme_image_attach_body code from image_attach.module, which seems to work pretty well.

if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    if ($node->iid) {
        $img_size = variable_get('image_attach_size_body_'. $node->type, IMAGE_ORIGINAL);
        if ($img_size != IMAGE_ATTACH_HIDDEN) {
            $image = node_load($node->iid);
            if (!node_access('view', $image)) {
                // If the image is restricted, don't show it as an attachment.
                return NULL;
            }
            $output = '<div class="image-attach">';
            $output .= l(image_display($image, $img_size), "node/$node->iid", array(), NULL, NULL, FALSE, TRUE);
            $output .= '</div>'."\n";
 
            print $output;
        }
    }
}

Show node attachments in a block

If you want to show the attachments of a specific node in a block you can use the following code snippets.

  // We load the node and the attached files by the nid (here nid = 26)
  $files = upload_load(node_load(26));
  $rows = array();
  foreach ($files as $file) {
    if ($file->list) {
      $href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
      $text = $file->description ? $file->description : $file->filename;
      $mime = strtoupper(substr($file->filemime, strrpos($file->filemime, '/')+1));
      $rows[] = array(l($text, $href), format_size($file->filesize), $mime);
    }
  }
 
  if (count($rows)) {
    print theme('table', $header, $rows, array('id' => 'attachments'));
  }