Drupal 7 Fields/CCK: Using the File Field Modules

Exclusive offer: get 50% off this eBook here
Drupal 7 Fields/CCK Beginner's Guide

Drupal 7 Fields/CCK Beginner's Guide — Save 50%

Explore Drupal 7 fields/CCK and master their use

$26.99    $13.50
by Dave Poon | July 2011 | Open Source

Drupal is not only an open source CMS, but is also a web application development framework. The extensible and flexible architecture of Drupal can be used for any size of business including freelancers, agencies, governments, universities, and enterprises. The White House, Digg, Fast Company, Sony Music, McDonald's, and Stanford University are all using Drupal for their sites.

One of the most important milestones in Drupal was the Content Construction Kit (CCK). Most of this module has been integrated into Drupal 7 core. It is now called Field API. So when you install Drupal 7, it will come with this powerful module in core, and you can start to create custom fields for your site without downloading an extra module.

In this article by Dave Poon, author of Drupal 7 Fields/CCK Beginner's Guide, we shall learn about:

  • Adding and configuring file fields to content types
  • Using file fields

 

Drupal 7 Fields/CCK Beginner's Guide

Drupal 7 Fields/CCK Beginner's Guide

Explore Drupal 7 fields/CCK and master their use

        Read more about this book      

(For more resources on Drupal, see here.)

Adding and configuring file fields to content types

There are many cases where we need to attach files to website content. For instance, a restaurant owner might like to upload their latest menu in PDF format to their website, or a financial institution would like to upload a new product catalog so customers can download and print out the catalog if they need it.

The File module is built into the Drupal 7 core, which provides us with the ability to attach files to content easily, to decide the attachment display format, and also to manage file locations.

Furthermore, the File module is integrated with fields and provides a file field type, so we can easily attach files to content using the already discussed field system making the process of managing files much more streamlined.

Time for action – adding and configuring a file field to the Recipe content type

In this section, we will add a file field to the Recipe content type, which will allow files to be attached to Recipe content. Follow these steps:

  1. Click on the Structure link in the administration menu at the top of the page.

    Drupal 7 Fields/CCK: Using the File Field Modules

  2. The following page will display a list of options. Click on the Content types link to go to the Content types administration page.

    Drupal 7 Fields/CCK: Using the File Field Modules

  3. Since we want to add a file field to the Recipe content type, we will click on the manage fields link on the Recipe row as shown in the following screenshot:

    (Move the mouse over the image to enlarge it.)

  4. This page will display the existing fields of the Recipe content type. In the Label field enter "File", and in the Field name field enter "file". In the field type select list select File as the field type, the field widget will be automatically switched to File as the field widget. After the values are entered, click on Save.

    Drupal 7 Fields/CCK: Using the File Field Modules

  5. A new window will pop up which provides the field settings for the file field that we are creating. There are two checkboxes, and we will enable both these checkboxes. The last radio button option will be selected by default. Then click on the Save field settings button at the bottom of the page.

    Drupal 7 Fields/CCK: Using the File Field Modules

  6. We clicked on the Save field settings button to store the values for the file field settings that we selected. After that, it will direct us to the file field settings administration page, as in the following screenshot:

    Drupal 7 Fields/CCK: Using the File Field Modules

  7. We can leave the Label field as default as it will be filled automatically with the value we entered previously. We will also leave the Required field as default, because we do not want to force users to attach files to every recipe. In the Help text field, we can enter "Attach files to this recipe".

    Drupal 7 Fields/CCK: Using the File Field Modules

  8. In the Allowed file extensions section, we can enter the file extensions that are allowed to be uploaded. In this case, we will enter "txt, pdf, zip". In the File directory section, we can enter the name of a subdirectory that will store the uploaded files, and in this case, we will enter "recipe_files":

    Drupal 7 Fields/CCK: Using the File Field Modules

  9. In the Maximum upload size section, we can enter a value to limit the file size when uploading files. We will enter "2MB" in this field. The Enable Description field checkbox allows users to enter a description about the uploaded files. In this case, we will enable this option, because we would like users to enter a description of the uploaded files.

    (Move the mouse over the image to enlarge it.)

  10. In the Progress indicator section, we can select which indicator will be used when uploading files. We select Throbber as the progress indicator for this field.

    (Move the mouse over the image to enlarge it.)

  11. You will notice the bottom part of the page is exactly same as in the previous section. We can ignore the bottom part and click on the Save settings button to store all the values we have entered.

    Drupal 7 Fields/CCK: Using the File Field Modules

  12. Drupal will direct us back to the manage fields administration page with a message saying we have successfully saved the configuration for the file field.

    Drupal 7 Fields/CCK: Using the File Field Modules

  13. After creating the file field, the file field row will be added to the table. This table will display the details about the file field we just created.

    (Move the mouse over the image to enlarge it.)

 

Drupal 7 Fields/CCK Beginner's Guide Explore Drupal 7 fields/CCK and master their use
Published: July 2011
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

 

        Read more about this book      

(For more resources on Drupal, see here.)

What just happened?

We have configured and added the file field for the Recipe content type.

In this section, we created a file field for the Recipe content type; we entered all the essential values for the file field such as the field label and field type. When we clicked on Save for the file field, the Field Settings page let us configure additional settings for the file field. The first option Enable Display field will provide a checkbox on the content form so we can specify if the file name is displayed on the content page. The second option Files displayed by default will cause the checkbox to be checked by default. If neither option is checked, the filename will be displayed on the content page. The last option is to choose the upload destination; currently the public file option is selected by default. If we have configured the private file system path on the file system administration page, we will see a private file option available on this page. Moreover, be cautious when saving the field settings, because the settings apply to the file field everywhere it is used, and they affect the way data is stored in the database and cannot be changed once data has been created.

The file field contains quite a lot of settings. The Label field, the Required field and the Help text field are the common settings for fields. The specified file field settings are Allowed file extensions, File directory, Maximum upload size, Enable Description field, and Progress indicator.

The Allowed file extensions settings will affect the type of file users can upload. If we only enter a "txt" extension for this field, users can only upload text files to content. This is not a setting found too often because it makes for too many limitations for users. So, in this case, we entered the "txt, zip, pdf" file extensions to allow users to upload files with some of the more common extensions.

In the File directory settings, we can enter the name of the subdirectory that will store the uploaded files. This field is optional, if we do not enter anything here, all the files will be stored in the sites/default/files directory by default.

Setting up some constraints for uploading files is necessary, because we cannot allow users to upload files without limitation, they might upload a 20 GB video file to our server, which absolutely wastes our server resources and is very cost ineffective. The maximum upload size settings can be used to set up the allowed file size, which can effectively limit the file sizes that users can upload.

When uploading files to the server, it would be better to have a progress indicator to present the status of file upload. Luckily, the file module already provides us with such a feature. It allows us to choose the type of progress indicator for the file field. There are two types of progress indicators that we can choose, Throbber and Bar with progress meter.

The Progress indicator option will only be available if the appropriate PHP extensions, either APC or PECL, are enabled on the server. Otherwise the Progress indicator section is not visible and neither is the throbber or the bar option.

Throbber will be used by default, which does not show the progress of file upload, but it saves more space on the form, and it will not need extra PHP configuration to make it work properly. Bar with progress meter can indicate the progress of file upload, but it will take more space on the form, and it is more complicated to set up. This is because we have to install extra PHP packages and extensions (installing APC or PECL), and we have to set up the PHP configurations (configuring PHP.ini) properly in order to make the Bar with progress meter indicator work.

Have a go hero – adding and configuring a file field to the Cooking Tip content type

It's time for a challenge. We have created a file field for the Recipe content type. We can use the same method we have learned here to add and configure a file field to the Cooking Tip content type. So the Cooking Tip content can provide files for people to download. You will need to make configurations so that users are allowed to upload PDF, TXT, ZIP, and DOC files, and limit the upload size to 2 MB.

Using file fields

We have gone through all the steps for creating the file field, and now, it's time to use what we have created.

Time for action – using the file field

In this section, we will make use of the file field when creating new Recipe content. Follow these steps:

  1. Click on the Add content link in the administration menu at the top of the page.

    Drupal 7 Fields/CCK: Using the File Field Modules

  2. This page displays a list of the existing content types. Because we would like to add recipe content, we click on the Recipe link to create new content.

    Drupal 7 Fields/CCK: Using the File Field Modules

  3. On this page we can enter new recipe content. In the Title field, we enter "Shortbread Biscuits". In the Body field, we enter the recipe details as in the following screenshot, or for your convenience you can copy and paste the content from the content.txt file if you have downloaded the project files.

    (Move the mouse over the image to enlarge it.)

  4. There are radio buttons in the Difficulty section. We select Intermediate for this recipe, and we enter "Biscuits" in the Recipe Category field. If you have used different type of field widgets for the same fields, you can still select or check your options.

    Drupal 7 Fields/CCK: Using the File Field Modules

  5. We will see there is a new section called File. This section provides a file widget that allows us to browse and upload a file to the website. We click on the Browse… button that will open a file browser; we can find a file through the file browser, and click on open to select the file. It does not actually upload any file until we click the Upload button.

    Drupal 7 Fields/CCK: Using the File Field Modules

  6. After clicking on the Upload button, it will upload the file you selected through the file browser, and we will see that the Throbber progress indicator is shown while uploading the file. After uploading the file, it will change the file field interface, and in the Description field, we enter "Shortbread Biscuits Recipe". Click on the Save button at the bottom of the page to store all the values we entered.

    Drupal 7 Fields/CCK: Using the File Field Modules

  7. Drupal directs us to the full content view of the new content, and it displays a message saying Recipe Shortbread Biscuits has been created successfully.

    Drupal 7 Fields/CCK: Using the File Field Modules

  8. In the full content view, we will see the output of the Title, Body, Difficulty, Recipe categories, and File fields as shown in the following screenshot. Users can now click on the file name link to download the file to their computer.

    Drupal 7 Fields/CCK: Using the File Field Modules

What just happened?

We have learned how to use the file field when creating new content.

We uploaded a file using the file field when creating new recipe content. When the file field is created for the Recipe content type, the existing recipe content allows you to attach files. If we navigate to one of our existing recipe contents, and click on the Edit tab, we will see the file field is also available on the Edit page of the content. Which means, when we add a new field to a content type, the ability to attach files to the existing content is there, so it is not only applied to the new content.

While uploading the file, we will see the progress indicator appear next to the Upload button. The type of progress indicator is based on what we selected on the file field settings administration page. Since we selected the throbber as the progress indicator, the throbber was shown while uploading the file. Perhaps we might not see the throbber clearly, if the file we uploaded was a small file. If we upload a bigger file, we can clearly see the throbber is running.

After uploading the file, Drupal will change the file field user interface. It will display the file name of the uploaded file and show the file size of the uploaded file.

Drupal 7 Fields/CCK: Using the File Field Modules

The Include file in display option allows us to select whether the file will be displayed in the content. If it is checked, the file will be displayed when people are viewing the content, otherwise it will be hidden. The Include file in display option is provided, because we have enabled this option in the file field settings administration page. If you remember, there are two options, one is Enable Display field, the other is Files displayed by default. That's the reason this option is provided and the Include file in display option is there and is enabled by default.

Drupal 7 Fields/CCK: Using the File Field Modules

Below the Include file in a display option, there is the Enable Description field, which is the option we configured in the file field settings administration page. We have enabled the description field, so the option is shown when creating or editing content. It is a good idea to enter a description of the uploaded file, otherwise it will use the file name as the description of the uploaded file. Sometimes this does not make much sense to people. If the file name of a file is 19228_epd.pdf, people will not understand what it is for.

Have a go hero – adding a file field to the Cooking Tip content type

It's time for a little challenge. We have uploaded a file using the file field when creating new recipe content. We can use the same method we have learned here to create new Cooking Tip content, and attach files to the Cooking Tip content. You can try different settings to see the differences between the options when attaching files.

Summary

In this article we have learned how to add file fields to content types, and we also learned how to configure file fields.

In the next article we will learn to use image field modules.


Further resources on this subject:


Drupal 7 Fields/CCK Beginner's Guide Explore Drupal 7 fields/CCK and master their use
Published: July 2011
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

About the Author :


Dave Poon

Dave Poon is a web developer and designer based in Sydney. He started his career as a freelance graphic and web designer in 1998 and works with web development agencies and medium-size enterprises. After graduating from Central Queensland University with a degree in Multimedia Studies and a Master's degree in IT he began his love affair with Drupal, and works for a variety of companies using Drupal.

Currently, he is the director of Erlango (http://erlango.com), a web product development startup, located in Sydney and Hong Kong, which is using Drupal as a framework to create web products and customized CMS.

He was the technical reviewer for Drupal Intranets with Open Atrium, published by Packt Publishing.

Books From Packt


Drupal 7
Drupal 7

Drupal Intranets with Open Atrium
Drupal Intranets with Open Atrium

Drupal 7 First Look
Drupal 7 First Look

Drupal 7 Module Development
Drupal 7 Module Development

Drupal 7 Social Networking: RAW
Drupal 7 Social Networking: RAW

Drupal 7 Themes
Drupal 7 Themes

Drupal Web Services
Drupal Web Services

Drupal 6 Theming Cookbook
Drupal 6 Theming Cookbook


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
9
X
6
8
E
B
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software