Tải bản đầy đủ (.pdf) (288 trang)

Drupal 7 FieldsCCK beginner's guide (2011)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.13 MB, 288 trang )


Drupal 7 Fields/CCK
Beginner's Guide

Explore Drupal 7 fields/CCK and master their use

Dave Poon

BIRMINGHAM - MUMBAI


Drupal 7 Fields/CCK
Beginner's Guide

Copyright © 2011 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system,
or transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.

First published: July 2011

Production Reference: 1050711



Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849514-78-1
www.packtpub.com

Cover Image by Rakesh Shejwal ()


Credits
Author
Dave Poon
Reviewers
Maurice Green, PhD

Project Coordinator
Michelle Quadros
Proofreader
Kevin McGowan

Abdul Hafeez
Dan Stefan Poka

Indexer
Tejal Daruwale

Acquisition Editor
Usha Iyer


Graphics
Nilesh Mohite

Development Editor
Neha Mallik

Production Coordinator
Aparna Bhagat

Technical Editor
Vanjeet D'souza

Cover Work
Aparna Bhagat

Copy Editor
Neha Shetty


About the Author
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 sized enterprises. He graduated from Central Queensland University with a
degree in Multimedia Studies and a Master's degree in IT. His love affair with Drupal began
afterwards, and he now works for a variety of companies using Drupal.
He is the founder and Technical Director of Erlango (), a web
product development startup, located in Sydney and Hong Kong, which uses Drupal
as a framework to create web products and customized CMS.
He is also the technical reviewer of Drupal Intranets with Open Atrium by Tracy Smith

(Packt Publishing, 2011).
I would like to thank my wife Rita (she should be my wife at the time of the
printing of this book) for her endless patience and support. Without her,
what I do would be meaningless.
I would also like to thank my father for his continued encouragement.
It is my great fortune to have worked with a great team of publishing
professionals at Packt Publishing. I extend my sincerest gratitude to Usha
Iyer, Neha Mallik, Vanjeet D'souza, Neha Shetty, and Michelle Quadros for
their cooperation and help in writing this book.


About the Reviewers
Maurice Green (PhD) began his love affair with computers in 1963 as a graduate student
in biochemistry using Fortran and punch cards. Retiring after a 40 year career as a medical
laboratory director specializing in automated laboratory instrumentation and information
systems and an IT manager in the Department of Medicine at Stanford University, he turned
his attention to photography, video editing, and web design.
He is currently the president of the Silicon Valley Computer Society and leads the Web
Design special interest group. He is the developer and webmaster for the Silicon Valley
Computer Society website ( He presented several talks at the Design
4 Drupal conference at Stanford. He was the reviewer for Drupal 7 First Look by Mark Noble
(Packt Publishing, 2010).

Abdul Hafeez is a professional software developer, technology consultant, and an
avid Drupal user as well as developer. He has completed his Bachelor's degree in
Information Technology.
Abdul has spent a number of years in the development of software projects specifically in
Drupal. He has provided patches and support for many contributed Drupal modules and has
been involved with the Drupal community since 2008. He has developed and maintained
many custom modules for clients. Abdul's Drupal user account (rightchoice2c_me) can be

found at />Abdul is focused on the integration and interoperability of different technologies, especially
ones related to PHP, Ruby on Rails, and the web. He is an active member in the open source
community, especially in Drupal.
I would like to thank my parents and my friends, for their encouragement
and for giving me time to pursue and support Drupal-related tasks,
including reviewing this book.


Dan Stefan Poka is an avid Drupal developer, active mainly in the London (UK) area,
with roughly three years' Drupal experience, having been involved in the community since
Drupal 5. He acquired this experience by working in small teams and big teams alike and
technologies as different as Ubercart and ApacheSolr. He feels comfortable taking on all
parts of Drupal development from module development to theme development.
His first experience in reviewing was for the well known Pro Drupal Development (Second
Edition) from Apress by sending corrections to code examples, all of which made it into the
book's errata.
I would like to thank my colleagues from whom I've learned a lot, and
my family for their patience.


www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related
to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub
files available? You can upgrade to the eBook version at www.PacktPub.com and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up
for a range of free newsletters and receive exclusive discounts and offers on Packt books

and eBooks.



Do you need instant solutions to your IT questions? PacktLib is Packt’s online digital book
library. Here, you can access, read and search across Packt's entire library of books. 

Why Subscribe?
‹‹ Fully searchable across every book published by Packt
‹‹ Copy and paste, print and bookmark content
‹‹ On demand and accessible via web browser

Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view nine entirely free books. Simply use your login credentials for
immediate access.



Table of Contents
Preface
Chapter 1: Nodes and Content types in Drupal 7
The book example project
A preview of the final project
What is a content type?
Default content types in Drupal 7 core
The Basic page content type
The Article content type
Time for action – creating contents using the article content type
Creating custom content types

Creating the Cooking Tip content type
Time for action – creating the Cooking Tip content types
Creating the Recipe content type
Time for action – creating the Recipe content type
Adding content using custom content types
Adding content using the Cooking Tip content type
Time for action – adding content using the Cooking Tip content type
Adding content using the Recipe content type
Time for action – adding contents using the Recipe content type
Summary

Chapter 2: Fields
What is CCK?
Fields in Drupal 7
Enabling the Field module
Time for action – enabling the Field module
Field, Field UI, and Field SQL storage modules
Time for action – finding the Field UI
Field types and input widgets

1
7
8
8
13
14
15
16
16
20

20
20
24
25
27
27
27
30
30
32

33
33
35
35
35
37
38
40


Table of Contents

Time for action – finding the field types and input widgets in Field UI
Drupal 7 core default fields
Drupal 7 core field related modules
Number modules
Text modules
List modules
Taxonomy modules

Image modules
File modules

40
42
42
42
43
44
44
45
45

Adding fields to content types
Adding text fields using the text module
Time for action – adding Long text fields using the text module
Text fields
Time for action – adding text fields using the text module
Rearranging the order of fields
Rearranging the order of the Notes field
Time for action – rearranging the order of the Notes field
Adding fields to comments
Adding text fields to comments
Time for action – adding a text field to comments
Time for action – rearranging the order of the Email field
Reusing existing fields
Time for action – reusing the Notes field for the Recipe content type
Time for action – reusing the Source field for the Recipe content type
Time for action – rearranging the order of the Notes field
Time for action – reusing the Email text field in the Recipe content type

Time for action – rearranging the order of the Email field
Summary

Chapter 3: Multiple Value Fields

46
46
46
50
51
53
53
53
55
55
55
58
59
59
60
61
62
62
64

65

Enabling multiple values
Time for action – enabling multiple values
Adding Boolean fields

Time for action – adding a Boolean field
Setting default values
Time for action – setting default values
Adding list fields
Time for action – adding list fields
Multiple value widgets
Time for action – changing the number of the values
Time for action – checkboxes/radio buttons widget with multiple values
Summary
[ ii ]

65
66
72
72
75
75
78
78
83
86
87
90


Table of Contents

Chapter 4: Taxonomy

91


What is taxonomy?
Creating vocabularies
Time for action – creating the Cooking Tip Category vocabulary
Time for action – creating the Recipe Category vocabulary
Adding terms to vocabularies
Time for action – adding terms to Cooking Tip Category
Adding term references to content types
Time for action – adding a term reference field to the Cooking Tip content type
Categorizing content using taxonomy
Time for action – categorizing Cooking Tip Content using taxonomy
Adding fields to vocabularies
Time for action – adding a field to the Cooking Tip Category vocabulary
Summary

Chapter 5: File and Image Fields

91
93
94
96
98
98
102
103
108
108
113
113
118


119

Adding and configuring file fields to content types
Time for action – adding and configuring a file field to the Recipe content type
Using file fields
Time for action – using the file field
Adding image fields to content types
Time for action – adding an image field to the Recipe content type
Configuring image field settings
Time for action – configuring an image field for the Recipe content type
Creating custom image styles
Time for action – creating a custom image style
Summary

Chapter 6: Managing Field Display

120
120
128
128
133
134
135
136
141
141
146

147


Field display
Time for action – a first look at the field display settings
Formatting field display in the Teaser view
Time for action – formatting the Body field display in teaser view
Custom display settings
Time for action – enabling the Full content view mode in custom display settings
Formatting the image field display on Full content
Time for action – formatting the image field display on Full content
Formatting the comment field display

[ iii ]

148
148
153
153
157
158
160
160
163


Table of Contents

Time for action – formatting the comment field display
Formatting the vocabulary field display
Time for action – formatting the vocabulary field display
Summary


Chapter 7: Contributed Field Modules

163
165
165
167

169

Drupal modules
Contributed modules
Finding modules
Time for action – finding contributed field modules through the Drupal website
Time for action – finding contributed field modules through the
drupalmodules.com website
Installing and enabling contributed modules
Time for action – installing a module
Time for action – installing a module using Update manager
Contributed field modules
Email field module
Link module
Table field module
Computed field module
Content Templates (Contemplate) module
Summary

Chapter 8: Field Level Permissions

170

170
171
171
173
176
176
180
184
184
185
185
185
185
185

187

Downloading the Field Permission module
Time for action – downloading the Field Permission module
Installing and enabling the Field Permission module
Time for action – installing and enabling the Field Permission module
Enabling field-level permissions
Time for action – enabling field-level permissions
Configuring field-level permissions on the PERMISSIONS administration page
Time for action – configuring field-level permissions on the PERMISSIONS
administration page
Field-level permission reports
Time for action – the Field permissions report page
Summary


Chapter 9: Theming Fields

188
188
189
190
192
192
195
196
199
199
202

203

What is a theme?
What is theming?
How Drupal renders a webpage
How to get started on theming
Creating a Drupal theme

204
204
205
206
207
[ iv ]



Table of Contents

Time for action – creating a Drupal theme by cloning the existing theme
Overriding template files
Time for action – creating a template file for the Recipe content type
Editing template files
Time for action – editing a template file
Styling the page using CSS
Time for action – styling the page using CSS
Summary

207
211
212
214
214
221
221
224

Chapter 10: A Database Perspective of Fields

225

Key elements in Fields
Entities
Bundles
Field storages
Field types
Fields

Field instances
Field widgets
Field formatters
Fieldable entities
Taxonomy
New concepts in Drupal 7
Understanding nodes in Drupal
Turning everything into a node
Is everything an entity?
Entities are the new nodes?
Are they entities or objects?
A database perspective of fields
Content types
Time for action – creating a new content type
Fields
Field-related database tables
Summary

226
226
226
226
226
227
227
227
227
227
228
228

229
229
230
230
231
232
232
234
238
240
250

Pop Quiz Answers

251

Chapter 1, Nodes and Content types in Drupal 7
Pop quiz – understanding the default content types
Pop quiz – creating content types
Pop quiz – adding content using custom content types

[v]

251
251
251
251


Table of Contents


Chapter 2, Fields
Pop quiz – enabling the Field module
Pop quiz – finding the Field UI
Pop quiz – finding the field types and input widgets in Field UI
Pop quiz – adding text fields using the text module
Pop quiz – rearranging the order of fields
Pop quiz – adding text fields to comments
Pop quiz – rearranging the order of fields in comments
Pop quiz – reusing fields in content types
Pop quiz – reusing fields in content types
Chapter 3, Multiple Value Fields
Pop quiz – multiple values in fields
Pop quiz – understanding the Boolean field
Pop quiz – setting default values
Pop quiz – adding list fields
Pop quiz – understanding multiple value widgets
Chapter 4, Taxonomy
Pop quiz – creating vocabularies
Pop quiz – adding terms to vocabularies
Pop quiz – adding term references to content types
Pop quiz – categorizing content using taxonomy
Pop quiz – adding fields to vocabularies
Chapter 5, File and Image Fields
Pop quiz – adding and configuring file field settings
Pop quiz – using the file field
Pop quiz – configuring image field settings
Pop quiz – creating custom image styles
Chapter 6, Managing Field Display
Pop quiz – a first look at the field display settings

Pop quiz – formatting field display in the teaser
Pop quiz – custom display settings
Pop quiz – formatting image field display on Full content
Pop quiz – formatting comment field display
Pop quiz – formatting vocabulary field display
Chapter 7, Contributed Field Modules
Pop quiz – finding a contributed field module
Pop quiz – installing and enabling contributed modules

[ vi ]

252
252
252
252
252
252
252
253
253
253
253
253
253
253
254
254
254
254
254

254
255
255
255
255
255
255
256
256
256
256
256
256
257
257
257
257
257


Table of Contents

Chapter 8, Field Level Permissions
Pop quiz – installing and enabling the Field Permission module
Pop quiz – enabling field-level permissions
Pop quiz – configuring field-level permissions on the PERMISSIONS
administration page
Pop quiz – the Field permissions report page
Chapter 9, Theming Fields
Pop quiz – creating a Drupal theme by cloning the existing theme

Pop quiz – creating a template file for the Recipe content type
Pop quiz – editing a template file
Pop quiz – styling the page using CSS
Chapter 10, A Database Perspective of Fields
Pop quiz – key elements in Fields
Pop quiz – new concepts in Drupal 7
Pop quiz – creating a new content type
Pop quiz – a database perspective of fields

Index

257
257
258
258
258
258
258
258
259
259
259
259
259
260
260

261

[ vii ]




Preface
Drupal is changing the way we create a feature and content-rich website. When I created
my first web-based Content Management System (CMS) website in Drupal, I was amazed
at the power of the Drupal CMS system. It allows me to rapidly create a high quality web
application in a short period of time without sacrificing the application functionality.
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.
The Field API module is not just the CCK module moved to the Drupal core. It has been
completely rewritten and redesigned in Drupal 7. It has been optimized to run faster and
work more efficiently. It provides you with more field type options, and it has given you more
flexible options to allow you to create custom fields not only for content types, but also for
comments, taxonomy, and more, that was not possible to achieve before Drupal 7. After
reading this book, you will learn how the new Fields API module works, and understand the
concept of fields and how to implement them.
In this book, we will use Drupal 7 fields to build a fictional website from scratch. We will
explore the power of fields within Drupal 7, then you can apply these techniques to your
own projects.


Preface


What this book covers

Chapter 1, Nodes and Content Types in Drupal 7 explains the concept of nodes and content
types in Drupal. It will explain the default content types that Drupal 7 core comes with.
This chapter will also teach you how to create contents and create custom content types
in Drupal.
Chapter 2, Fields explains the concept of the new core module in Drupal 7 and demonstrates
to how to add fields to content types, rearrange the order of fields, and reuse existing fields.
It will also show how to add fields to comments, which is new in Drupal 7.
Chapter 3, Multiple Value Fields focuses on using field modules to creating multiple value
fields for the site. We will look in detail at the new field type and other multiple value field
type options such as Boolean, List, List (Numeric), and List (Text) field types.
Chapter 4, Taxonomy focuses on using fields in taxonomy; at the beginning of the
chapter we will learn what taxonomy is. We will then create custom vocabularies, and add
taxonomy terms. We will also look at how to add fields to vocabularies and term references
to content types.
Chapter 5, File and Image Fields focuses on using the file and image fields modules. We will
look at adding file and image fields to content types, configuring file and image field settings,
using and creating image styles, as well as adding those fields to content types.
Chapter 6, Managing Field Display focuses on field display management. We will look at
how we control field display on the pages, format field display on teaser and full content
pages, customize display settings, format comment fields display, and format vocabulary
fields display.
Chapter 7, Field Modules focuses on using field related modules. We will learn where to find
the contributed modules and field related modules, we will also learn about downloading
modules and installing them. We will also look at some of the most popular field related
modules such as the Content Templates module and Field permission modules.
Chapter 8, Field Level Permissions focuses on using the field permission module. We
will discuss how to use this powerful module to control the permissions of fields. At the

beginning of this chapter, we will start from scratch, by downloading the module, installing
the module, and enabling it. Then we will continue with enabling permissions in fields,
viewing field-level permissions report and configuring permissions in the PERMISSIONS
administration page.
Chapter 9, Theming Fields focuses on themes. We will discuss what theming is. We will look
at where to find the template files for fields, creating template files, editing template files,
creating CSS files, and theming field display.

[2]


Preface

Chapter 10, A Database Perspective of Fields explains the difference between CCK fields
and the Drupal 7 fields in the database. This chapter also shows you how to find the content
types and fields we created in the example project from the database, and shows the
interaction between Drupal and the database.

What you need for this book

To follow along with the examples in this book, you will need a computer which can run
MySQL, PHP, and the Apache web server, which are all prerequisites for Drupal. Drupal is
written in PHP and can run on many platforms and environments including Windows, Mac
OS X, and Ubuntu Linux.
You may want to create an account with a website hosting company to test your work,
although you can also use a regular desktop or a laptop computer. But all that is required is a
development environment set up on your computer; some of the most popular development
environment bundles are Acquia Drupal, WAMP, XAMPP, and MAMP.
You will also need the Drupal 7 core, which is available at


Who this book is for

This book is designed primarily for people that have a basic knowledge of Drupal and want
to familiarize themselves with Field API to add fields to content types using the new Drupal
7 fields. However, technical users will also find the book useful, as we will explore a wide
variety of techniques that are used to build websites with fields.
Previous experience with website development or programming languages is not required,
although it is helpful to have some experience with using Drupal 7, and CSS will be useful for
some parts of this book.

Conventions

In this book, you will find several headings appearing frequently.
To give clear instructions of how to complete a procedure or task, we use a title section
like this:

Time for action – heading
Action 1
Action 2
Action 3
[3]


Preface

Instructions often need some extra explanation so that they make sense, so they are
followed with:

What just happened?
This heading explains the working of tasks or instructions that you have just completed.

You will also find some other learning aids in the book, including:

Pop quiz – heading
These are short multiple choice questions intended to help you test your own understanding.

Have a go hero – heading
These set practical challenges and give you ideas for experimenting with what you
have learned.
In this book, you will find a number of styles of text that distinguish between different
kinds of information. Here are some examples of these styles, and an explanation of
their meaning.
Code words in text are shown as follows: "Let's take a deeper look at each directive used in
the Drupal robots.txt file".
A block of code is set as follows:
// We hide the comments and links now so that
// we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
?>

When we wish to draw your attention to a particular part of a code block, the relevant lines
or items are set in bold:
// We hide the comments and links now so that
// we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);

?>
[4]


Preface

New terms and important words are shown in bold. Words that you see on the screen, in
menus or dialog boxes for example, appear in our text like this: " I added the site logo file
into the Test URLs box".
Warnings or important notes appear in a box like this.

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or may have disliked. Reader feedback is important for us to develop
titles that you really get the most out of.
To send us general feedback, simply drop an email to , and
mention the book title in the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the
SUGGEST A TITLE form on www.packtpub.com or email
If there is a topic that you have expertise in and you are interested in either writing or
contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you
to get the most from your purchase.


Downloading the example code
You can download the example code files for all Packt books you have purchased from
your account at . If you purchased this book elsewhere, you
can visit and register to have the files e-mailed
directly to you.

[5]


Preface

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes
do happen. If you find a mistake in one of our books—maybe a mistake in the text or
code—we would be grateful if you would report this to us. By doing so, you can save other
readers from frustration, and help us to improve subsequent versions of this book. If you
find any errata, please report them by visiting ktpub. com/support,
selecting your book, clicking on the let us know link, and entering the details of your errata.
Once your errata are verified, your submission will be accepted and the errata added to
any list of existing errata. Any existing errata can be viewed by selecting your title from
/>
Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media.
At Packt, we take the protection of our copyright and licenses very seriously. If you come
across any illegal copies of our works in any form on the Internet, please provide us with the
location address or web site name immediately so that we can pursue a remedy.
Please contact us at with a link to the suspected
pirated material.

We appreciate your help in protecting our authors, and our ability to bring you
valuable content.

Questions

You can contact us at if you are having a problem with any
aspect of the book, and we will do our best to address it.

[6]


1

Nodes and Content types in Drupal 7
At the beginning of this chapter, we will have a look at a preview of our final
project, it will give you a better idea of what we need to do, and what will be
achieved when we reach the end of this book. We will also explain the concept
of nodes and content types in terms of Drupal. After that we will get our hands
dirty with a hands-on tutorial on creating content and content types using
the default content types in Drupal. We will go even further to create our own
custom content types and add more content by using these custom content
types to create a website. Sounds cool! Let's get started!

We shall discuss the following topics in this chapter:
‹‹ The book example project
‹‹ A preview of the final project
‹‹ What is a content type?
‹‹ Default content types in Drupal 7 core
‹‹ Creating content using article content types
‹‹ Creating custom content types

‹‹ Adding content using custom content types


Nodes and Content types in Drupal 7

The book example project
This book will show you how to create a website using Drupal 7 and Fields. Instead
of explaining how the Fields module works in theory, we will learn about it by creating a
fictional project.
We will guide you through the complete project by following a set of steps or tasks, and thus
you will learn:
‹‹ How to create custom web content by creating your own content types and fields
‹‹ How to create fields for displaying multiple values
‹‹ How to create taxonomy vocabularies and terms for categorizing web content
‹‹ How to use the file field to provide file uploading functionality to the website
‹‹ How to use the image field to provide image uploading, extension validation, and
image processing functionality to the website
‹‹ How to format images using image styles
‹‹ How to control the display of fields in teasers and full content view modes
‹‹ How to set field level permissions for specific fields that only authenticated users
can access
‹‹ Finally, how to theme fields using templates and CSS
You also have a choice of downloading the final project file from www.packtpub.com
or www.drupal7fieldsbook.com to follow along with the book. Alternatively, you
can compare your project with the final project if you have any problem while following
the book.

A preview of the final project
The website that we are going to build in this book is a cooking website filled with recipes.
Let's take a look at some of the most important sections the website has, before starting

the project.

[8]


×