I'm using Drupal 5.1 with only the core modules.

I created a new content type for "Document" to use for hosting uploaded files. I turned off file upload for all content types other than Document, and configured everything correctly. This wasn't very hard, once I got used to the layout of the Drupal Administration menu.

Now, I want a link in my navigation menu called "Documents" which lists all the uploaded documents -- or at least the first 20. Should be easy, given that it was so easy to create the new content type, right?

Three hours later, I've finally managed to create it. I spent half an hour looking through the configuration options without finding such an option. Then I spent another 45 minutes trying to read the manual, and getting more and more frustrated. Go read the manual page on the "node" module, which specifies that you can create new content types, and then try to figure out from there how to SHOW that content. Good luck!

I finally found a PHP snippet that would show all content except for the "forums" type, which I could modify to show all uploads only. So far so good -- except only the title is shown, not the user name or date of the upload, which I'd like to show. I could pretty easily extend the SQL query, but trying to wade through the documentation to figure out how to display the user name was somewhat annoying -- and figuring out how to display the "changed date" in the user preferred format was really frustrating. There's no Theme function for formatting a date, there's no function with a name starting with "date", there's no Search field in the API function list, and even Google with a site: didn't give me much joy.

At last, I found format_date() in the docs, and it seems to be close enough for now. Here's what I came up with, for your enjoyment and critique:

function dox_title_list($result) {
  while ($node = db_fetch_object($result)) {
    $items[] = l($node->title, 'node/' . $node->nid, '') . 
      t(' updated !date by !name.',
        array('!date' => format_date($node->changed),
              '!name' => theme('username', $node)));
  }
  return theme('node_list', $items, $title);
}

$result = pager_query(db_rewrite_sql("SELECT n.nid as nid, n.title as title, n.type as type, n.changed as changed, u.name as name FROM {node} n INNER JOIN {users} u on n.uid = u.uid WHERE n.status = 1 AND n.type = 'dox' ORDER BY n.sticky DESC, n.changed DESC"));
$q = db_num_rows($result);
$output = dox_title_list($result);
$output .= "<div class='count'>$q documents.</div>";
print $output;

So, first: Am I really the first person who wants to create a list of all nodes of content type X? I would think that would have been a pretty common request?

Second: I'd appreciate it if someone with a clue could look over the above code and inspect it for n00by blunders and other badness.

Third: If someone else needs this same functionality, please feel free to steal this version.

Comments

vm’s picture

most people do this with views.module i think.

nancydru’s picture

When they added the ability to create new content types, they should have added the seemingly natural analog to "taxonomy/term/xxx" which would be "node/type/xxx" but they didn't. You are right, this should be there. But it would display teasers, not a title list.

Your code is probably as good as anything else. BTW, format_date has more functionality than the way you used it. And if that's not enough, try http://us2.php.net/manual/en/function.date.php

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

jwatte’s picture

You are right, this should be there. But it would display teasers, not a title list.

I'd take teasers, that's fine for my needs.

I'm aware of the PHP date function (I've written several systems in PHP before), but as the user may have set a preferred date format, I'm assuming I have to go through Drupal to get it properly localized.

nancydru’s picture