Drupal 7 Fields/CCK
Beginner's Guide
Explore Drupal 7 elds/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 transmied in any form or by any means, without the prior wrien permission of the
publisher, except in the case of brief quotaons embedded in crical arcles or reviews.
Every eort has been made in the preparaon of this book to ensure the accuracy of the
informaon presented. However, the informaon 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 informaon about all of the
companies and products menoned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this informaon.
First published: July 2011
Producon 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 (
)
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 Mulmedia Studies and a Master's degree in IT. His love aair with Drupal began
aerwards, 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 me of the
prinng of this book) for her endless paence and support. Without her,
what I do would be meaningless.
I would also like to thank my father for his connued encouragement.
It is my great fortune to have worked with a great team of publishing
professionals at Packt Publishing. I extend my sincerest gratude to Usha
Iyer, Neha Mallik, Vanjeet D'souza, Neha Shey, and Michelle Quadros for
their cooperaon and help in wring this book.
About the Reviewers
Maurice Green
(PhD) began his love aair with computers in 1963 as a graduate student
in biochemistry using Fortran and punch cards. Rering aer a 40 year career as a medical
laboratory director specializing in automated laboratory instrumentaon and informaon
systems and an IT manager in the Department of Medicine at Stanford University, he turned
his aenon to photography, video eding, 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
I would like to thank my colleagues from whom I've learned a lot, and
my family for their paence.
www.PacktPub.com
Support les, eBooks, discount offers and more
You might want to visit
www.PacktPub.com
for support les and downloads related
to your book.
Did you know that Packt oers eBook versions of every book published, with PDF and ePub
les available? You can upgrade to the eBook version at
www.PacktPub.com
and as a print
book customer, you are entled 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 collecon of free technical arcles, sign up
for a range of free newsleers and receive exclusive discounts and oers on Packt books
and eBooks.
Do you need instant soluons to your IT quesons? PacktLib is Packts online digital book
library. Here, you can access, read and search across Packt's enre 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
Time for acon – enabling the Field module 35
Field, Field UI, and Field SQL storage modules 37
Time for acon – nding the Field UI 38
Field types and input widgets 40
Table of Contents
[
ii
]
Time for acon – nding the eld types and input widgets in Field UI 40
Drupal 7 core default elds 42
Drupal 7 core eld related modules 42
Number modules 42
Text modules 43
List modules 44
Taxonomy modules 44
Image modules 45
File modules 45
Adding elds to content types 46
Adding text elds using the text module 46
Time for acon – adding Long text elds using the text module 46
Text elds 50
Time for acon – adding text elds using the text module 51
Rearranging the order of elds 53
Rearranging the order of the Notes eld 53
Time for acon – rearranging the order of the Notes eld 53
Adding elds to comments 55
Adding text elds to comments 55
Time for acon – adding a text eld to comments 55
Time for acon – rearranging the order of the Email eld 58
Reusing exisng elds 59
Adding term references to content types 102
Time for acon – adding a term reference eld to the Cooking Tip content type 103
Categorizing content using taxonomy 108
Time for acon – categorizing Cooking Tip Content using taxonomy 108
Adding elds to vocabularies 113
Time for acon – adding a eld to the Cooking Tip Category vocabulary 113
Summary 118
Chapter 5: File and Image Fields 119
Adding and conguring le elds to content types 120
Time for acon – adding and conguring a le eld to the Recipe content type 120
Using le elds 128
Time for acon – using the le eld 128
Adding image elds to content types 133
Time for acon – adding an image eld to the Recipe content type 134
Conguring image eld sengs 135
Time for acon – conguring an image eld for the Recipe content type 136
Creang custom image styles 141
Time for acon – creang a custom image style 141
Summary 146
Chapter 6: Managing Field Display 147
Field display 148
Time for acon – a rst look at the eld display sengs 148
Formang eld display in the Teaser view 153
Time for acon – formang the Body eld display in teaser view 153
Custom display sengs 157
Time for acon – enabling the Full content view mode in custom display sengs 158
Formang the image eld display on Full content 160
Time for acon – formang the image eld display on Full content 160
Formang the comment eld display 163
Table of Contents
Time for acon – enabling eld-level permissions 192
Conguring eld-level permissions on the PERMISSIONS administraon page 195
Time for acon – conguring eld-level permissions on the PERMISSIONS
administraon page 196
Field-level permission reports 199
Time for acon – the Field permissions report page 199
Summary 202
Chapter 9: Theming Fields 203
What is a theme? 204
What is theming? 204
How Drupal renders a webpage 205
How to get started on theming 206
Creang a Drupal theme 207
Table of Contents
[
v
]
Time for acon – creang a Drupal theme by cloning the exisng theme 207
Overriding template les 211
Time for acon – creang a template le for the Recipe content type 212
Eding template les 214
Time for acon – eding a template le 214
Styling the page using CSS 221
Time for acon – styling the page using CSS 221
Summary 224
Chapter 10: A Database Perspecve of Fields 225
Key elements in Fields 226
Enes 226
Bundles 226
Field storages 226
Pop quiz – nding the Field UI 252
Pop quiz – nding the eld types and input widgets in Field UI 252
Pop quiz – adding text elds using the text module 252
Pop quiz – rearranging the order of elds 252
Pop quiz – adding text elds to comments 252
Pop quiz – rearranging the order of elds in comments 253
Pop quiz – reusing elds in content types 253
Pop quiz – reusing elds in content types 253
Chapter 3, Mulple Value Fields 253
Pop quiz – mulple values in elds 253
Pop quiz – understanding the Boolean eld 253
Pop quiz – seng default values 253
Pop quiz – adding list elds 254
Pop quiz – understanding mulple value widgets 254
Chapter 4, Taxonomy 254
Pop quiz – creang vocabularies 254
Pop quiz – adding terms to vocabularies 254
Pop quiz – adding term references to content types 254
Pop quiz – categorizing content using taxonomy 255
Pop quiz – adding elds to vocabularies 255
Chapter 5, File and Image Fields 255
Pop quiz – adding and conguring le eld sengs 255
Pop quiz – using the le eld 255
Pop quiz – conguring image eld sengs 255
Pop quiz – creang custom image styles 256
Chapter 6, Managing Field Display 256
Pop quiz – a rst look at the eld display sengs 256
Pop quiz – formang eld display in the teaser 256
Pop quiz – custom display sengs 256
Pop quiz – formang image eld display on Full content 256
at the power of the Drupal CMS system. It allows me to rapidly create a high quality web
applicaon in a short period of me without sacricing the applicaon funconality.
Drupal is not only an open source CMS, but is also a web applicaon development
framework. The extensible and exible architecture of Drupal can be used for any size of
business including freelancers, agencies, governments, universies, 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 Construcon 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 elds 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 rewrien and redesigned in Drupal 7. It has been opmized to run faster and
work more eciently. It provides you with more eld type opons, and it has given you more
exible opons to allow you to create custom elds not only for content types, but also for
comments, taxonomy, and more, that was not possible to achieve before Drupal 7. Aer
reading this book, you will learn how the new Fields API module works, and understand the
concept of elds and how to implement them.
In this book, we will use Drupal 7 elds to build a conal website from scratch. We will
explore the power of elds within Drupal 7, then you can apply these techniques to your
own projects.
Preface
[
2
]
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.
Preface
[
3
]
Chapter 10, A Database Perspecve of Fields explains the dierence between CCK elds
and the Drupal 7 elds in the database. This chapter also shows you how to nd the content
types and elds we created in the example project from the database, and shows the
interacon 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
wrien in PHP and can run on many plaorms and environments including Windows, Mac
OS X, and Ubuntu Linux.
You may want to create an account with a website hosng 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 elds to content types using the new Drupal
7 elds. However, technical users will also nd the book useful, as we will explore a wide
variety of techniques that are used to build websites with elds.
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 nd several headings appearing frequently.
To give clear instrucons of how to complete a procedure or task, we use a tle secon
hide($content['comments']);
hide($content['links']);
print render($content);
?>
When we wish to draw your aenon to a parcular part of a code block, the relevant lines
or items are set in bold:
<?php
// We hide the comments and links now so that
// we can render them later.
hide($content['comments']);
hide($content['links']);
print render($content);
?>
Preface
[
5
]
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 le
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
tles that you really get the most out of.
To send us general feedback, simply drop an email to
, and
menon the book tle in the subject of your message.
nd any errata, please report them by vising
ktpub. com/support
,
selecng your book, clicking on the let us know link, and entering the details of your errata.
Once your errata are veried, your submission will be accepted and the errata added to
any list of exisng errata. Any exisng errata can be viewed by selecng your tle from
/>.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media.
At Packt, we take the protecon 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
locaon 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 protecng 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.
1
Nodes and Content types in Drupal 7
At the beginning of this chapter, we will have a look at a preview of our nal
project, it will give you a beer 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. Aer that we will get our hands
dirty with a hands-on tutorial on creang content and content types using
can access
Finally, how to theme elds using templates and CSS
You also have a choice of downloading the nal project le from
www.packtpub.com
or
www.drupal7fieldsbook.com
to follow along with the book. Alternavely, you
can compare your project with the nal project if you have any problem while following
the book.
A preview of the nal project
The website that we are going to build in this book is a cooking website lled with recipes.
Let's take a look at some of the most important secons the website has, before starng
the project.