Drupal file list

From WolfTech
Revision as of 12:55, 29 February 2008 by Jjrobert (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

How To Create A Taxonomy Based File List In Drupal

This page will guide you in setting up a list of uploaded files in Drupal. Hopefully the information contained in this tutorial will be as deep as Drupal's learning curve is high (if ever that were possible).

Create File Content Type

To begin, you should have administrative privileges and the Views and Taxonomy modules should be installed.

Bring up your Administer menu and navigate to Content Types under the Content Management category. We need to create a new content type to hold our uploaded files, so click Add Content Type near the top of the screen. Under the Identification tab, name the content type 'File' of type 'file' and write a brief description such as, "A node to hold an attached file." Skip the Submission Form tab. Under the Workflow tab, make sure that the Default Comment Setting is set to 'Disabled' and Attachments are enabled. Save your new content type.

Cool, you've created a content type to hold your uploaded files.

Creating And Populating A Vocabulary

We are about to define a list of terms (a vocabulary) to define our new File content type.

Adding A New Vocabulary

Return to the Administer menu, navigate to Categories, again under the Content Management category, and click on Add Vocabulary near the top of the screen. Name your new vocabulary 'File Type' and add a description such as, "Different categories of files." The Types option box should only recognize the content type 'File' and term Hierarchy should be disabled. Before saving, make sure you check the Required option, so that every File content type will have an associated word in the vocabulary. Go ahead and submit your vocabulary.

Generating Terms

Now that we have created a vocabulary, it will show up on the Categories page under the name 'File Type'. To populate the new vocabulary, click the Add Term link under the 'File Type' tab. Give the term a Name and a short Description before submitting it. You can repeat this step to add any other 'File' terms to your vocabulary. Once finished, you can click List Terms under 'File Type' on the Categories page to view or edit the terms you have added to the vocabulary.

Creating A List View

Now that we have created a 'File' type to hold our files and a vocabulary to group them, we must make a View to display them.

Defining The View

Return to the Administer menu and click on Views under the Site Building category. There should already be a few views listed, but we want to create a new one. Click on Add near the top of the screen. Name your view 'file_view', define the view's access levels, and add a short description such as, "Lists all file content types." Under the Page tab, check Provide Page View, give the view a URL such as 'view/files', and select 'List View' from the drop-down box under View Type.

Defining The Fields

Under the Fields tab, we need to create three fields to sort the data. From the Add Field drop-down box, select 'Node: Title' and click Add Field. Now that the field has been created, we need to adjust its settings. Under Option select 'As Link' and under Default Sort select 'Ascending' when the new field appears in the Fields table. Create two more fields, a 'Taxonomy: Term' and a 'File: Name', and set their Handler drop-downs to 'With Link' and 'With Download Link', respectively.

Defining The Filters

Under the Filters tab, we need to create a filter that lists our files by their vocabulary word. From the Add Filter drop-down box, select 'Taxonomy: Term' and click Add Filter. Now that we have created the filter, we need to adjust its settings. Under Operator select 'Is One Of' and select all 'file' Values that need to be displayed.

When you're finished, save the view. You should be able to access your list at:

{Your Drupal URL}/?q=view/files

Adjusting A View's Theme

Almost there! Now that we have a content type, vocabulary, and view, we need to adjust the view to display the files by their vocabulary word and node name. To do this, we'll have to deal with PHP code that will tell the theme what to display.

Generating The Code

Return to the Views page, by way of the Site Building category on the Administer menu. You should see your view listed in the Existing Views table. To modify the theme, click on the Theme Wizard link near the top of the page. Select your view's name in the Select A View drop-down box ('file_view', in the case of this example), select Theme Type 'Grouped List', and click Select Theme Type. From the Field On Which To Group box, select 'term_data_name' and click Generate Theme. You should see three text boxes containing code that will be added to your Drupal theme folder.

Editing The PHP Files

To access the correct files, you will need to know what theme you are currently using. To do this, navigate to Themes under the Site Building category on the Administer menu. Once you have determined your default theme, open your source Drupal folder and then to /themes/(your default theme)/. Once in the correct folder, open the file 'template.php' and insert the code generated in the first text box by the wizard at the end of the file. Once finished, create a file called 'views-list-file_view.tpl.php' and one called 'views-list-file_view.css'. Paste the correct code into each file.

Modifying The TPL File

In the 'views-list-file_view.tpl.php' file, we need to modify the code to get the display to work correctly. Replace the code portion of this file with the following:

 <?php

        drupal_add_css(path_to_theme() .'/views-list-file_view.css');

	$title = preg_replace("#<a[^>]*>([^<]*)</a>#", "$1", $title);
	$filename = preg_replace("#(<a[^>]*>)[^<]*(</a>)#", "$1{$title}$2", $filename);
  
  ?>


<div class="view-label view-field-filename">
  <?php print $filename_label?>
</div>
<div class="view-field view-data-filename">
  <?php print $filename?>
</div>

 

The name and link of the nodes and files are being passed together, so we separate them using regular language and concatenate them together to form a link to the file, with the name of the file's node. Then, we display that link with a label, under the vocabulary term that is handled by the 'template.php' file.

That's it! Everything should be displaying correctly.