How do I get attchments to display in reverse order?
I would like to add a monthly publication to a page and it would be great if the latest issue was at the top of the list (at the moment it would automatically be added to the bottom of the attachment list).

Comments

wmostrey’s picture

Put the following in template.php (I'm assuming you're using the upload module and thus slightly altering theme_upload_attachments)

function phptemplate_upload_attachments($files) {
  $header = array(t('Attachment'), t('Size'));
  $rows = array();
  foreach ($files as $file) {
    $file = (object)$file;
    if ($file->list && !$file->remove) {
      // Generate valid URL for both existing attachments and preview of new attachments (these have 'upload' in fid)
      $href = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
      $text = $file->description ? $file->description : $file->filename;
      $rows[] = array(l($text, $href), format_size($file->filesize));
    }
  }
  if (count($rows)) {
    $rows = array_reverse($rows);
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}
mooffie’s picture

or you could simply reverse the array and then call the "base class":

function phptemplate_upload_attachments($files) {
  return theme_upload_attachments(array_reverse($files));
}
marist89’s picture

Sweet! That worked for me. Note: the modification goes to the theme/theme_name/template.php file.

offal’s picture

This is exactly what I need but for some reason it's not working for me. What am I doing wrong?
I've added the lines to the bottom of the template .php in themes/garland/ (which is the theme I'm using, of course)
So my edited file now looks like this:

<?php
/**
 * Sets the body-tag class attribute.
 *
 * Adds 'sidebar-left', 'sidebar-right' or 'sidebars' classes as needed.
 */
function phptemplate_body_class($sidebar_left, $sidebar_right) {
  if ($sidebar_left != '' && $sidebar_right != '') {
    $class = 'sidebars';
  }
  else {
    if ($sidebar_left != '') {
      $class = 'sidebar-left';
    }
    if ($sidebar_right != '') {
      $class = 'sidebar-right';
    }
  }

  if (isset($class)) {
    print ' class="'. $class .'"';
  }
}

/**
 * Return a themed breadcrumb trail.
 *
 * @param $breadcrumb
 *   An array containing the breadcrumb links.
 * @return a string containing the breadcrumb output.
 */
function phptemplate_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
    return '<div class="breadcrumb">'. implode(' › ', $breadcrumb) .'</div>';
  }
}

/**
 * Allow themable wrapping of all comments.
 */
function phptemplate_comment_wrapper($content, $type = null) {
  static $node_type;
  if (isset($type)) $node_type = $type;

  if (!$content || $node_type == 'forum') {
    return '<div id="comments">'. $content . '</div>';
  }
  else {
    return '<div id="comments"><h2 class="comments">'. t('Comments') .'</h2>'. $content .'</div>';
  }
}

/**
 * Override or insert PHPTemplate variables into the templates.
 */

function _phptemplate_variables($hook, $vars) {
  if ($hook == 'page') {

    if ($secondary = menu_secondary_local_tasks()) {
      $output = '<span class="clear"></span>';
      $output .= "<ul class=\"tabs secondary\">\n". $secondary ."</ul>\n";
      $vars['tabs2'] = $output;
    }

    // Hook into color.module
    if (module_exists('color')) {
      _color_page_alter($vars);
    }
    return $vars;
  }
  return array();
}

/**
 * Returns the rendered local tasks. The default implementation renders
 * them as tabs.
 *
 * @ingroup themeable
 */
function phptemplate_menu_local_tasks() {
  $output = '';

  if ($primary = menu_primary_local_tasks()) {
    $output .= "<ul class=\"tabs primary\">\n". $primary ."</ul>\n";
  }

  return $output;
}
/**
 * Reverse order of uploaded files with upload module
 */

function phptemplate_upload_attachments($files) {
  $header = array(t('Attachment'), t('Size'));
  $rows = array();
  foreach ($files as $file) {
    $file = (object)$file;
    if ($file->list && !$file->remove) {
      // Generate valid URL for both existing attachments and preview of new attachments (these have 'upload' in fid)
      $href = file_create_url((strpos($file->fid, 'upload') === FALSE ? $file->filepath : file_create_filename($file->filename, file_create_path())));
      $text = $file->description ? $file->description : $file->filename;
      $rows[] = array(l($text, $href), format_size($file->filesize));
    }
  }
  if (count($rows)) {
    $rows = array_reverse($rows);
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}

I've tried both methods listed in this post, but no luck. Where have I gone wrong?

offal’s picture

Still looking for some help please. Why is none of this working for me (see my previous post)?

mooffie’s picture

(EDIT: ignore; duplicated reply.)

mooffie’s picture

(When you paste big chunks of code people are less likely to help you because they have to wade through all of it. You could have just pasted that last function ;-)

Perhaps you're not using the 'attachment' module? Other modules may have differnet theming function(s).

Replace that function with the following:

function phptemplate_upload_attachments($files) {
  return '***';
}

if you don't see the asterisks you know this function was ignored.

offal’s picture

Thanks mooffie.

Unfortunately I don't know enough about coding to know which bits to leave out. So, sorry about the lengthy code. I depend on good, helpful and willing folks like yourself to enable me to have my very humble website working - and for that help I am very very thankful.

I have discovered that the order is now reversed (using the same, unchanged code) but NOT when I am logged in as administrator. Is seems to only work when not logged in. Does anyone have any thoughts on that?

gibsonoliver’s picture

fantastic - that works really well, thanks for your help, Oliver.