Drupal 7 Fields_CCK - Beginner's Guide

twodotcuddlyInternet και Εφαρμογές Web

4 Δεκ 2013 (πριν από 3 χρόνια και 11 μήνες)

307 εμφανίσεις

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 (
shejwal.rakesh@gmail.com
)
Credits
Author
Dave Poon
Reviewers
Maurice Green, PhD
Abdul Hafeez
Dan Stefan Poka
Acquisition Editor
Usha Iyer
Development Editor
Neha Mallik
Technical Editor
Vanjeet D'souza
Copy Editor
Neha Shetty
Project Coordinator
Michelle Quadros
Proofreader
Kevin McGowan
Indexer
Tejal Daruwale
Graphics
Nilesh Mohite
Production Coordinator
Aparna Bhagat
Cover Work
Aparna Bhagat
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 (
http://erlango.com
), 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 (
http://svcs.net/
). 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
http://drupal.org/user/315349
.
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
service@packtpub.com
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.
http://PacktLib.PacktPub.com
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 pas
te, print and bookmark content

‹ On demand and accessible via web br
owser
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

1
Chapt
er 1: Nodes and Content types in Drupal 7

7
The book example project

8
A pre
view of the final project

8
What is a c
ontent type?

13
Def
ault content types in Drupal 7 core

14
The Basic page c
ontent type

15
The Article con
tent type

16
Time for action – cr
eating contents using the article content type

16
Crea
ting custom content types

20
Crea
ting the Cooking Tip content type

20
Time for action – cr
eating the Cooking Tip content types

20
Crea
ting the Recipe content type

24
Time for action – cr
eating the Recipe content type

25
Adding con
tent using custom content types

27
Adding con
tent using the Cooking Tip content type

27
Time for action – adding c
ontent using the Cooking Tip content type

27
Adding con
tent using the Recipe content type

30
Time for action – adding c
ontents using the Recipe content type

30
Summary

32
Chapter 2: Fields

33
What is CCK?

33
Fields in Drupal 7

35
Enabling the Field module

35
Time for action – enabling the Field module

35
Field, Field UI, and Field SQL st
orage modules

37
Time for action – finding the Field UI

38
Field types and input widge
ts

40
Table of Contents
[
ii
]
Time for action – finding the field types and input widgets in Field UI

40
Drupal 7 cor
e default fields

42
Drupal 7 cor
e field related modules

42
Number modules

42
Te
xt modules

43
List modules

44
Ta
xonomy modules

44
Image modules

45
File modules

45
Adding fields to content types

46
Adding te
xt fields using the text module

46
Time for action – adding Long t
ext fields using the text module

46
Te
xt fields

50
Time for action – adding t
ext fields using the text module

51
Rearr
anging the order of fields

53
Rearr
anging the order of the Notes field

53
Time for action – r
earranging the order of the Notes field

53
Adding fields to c
omments

55
Adding te
xt fields to comments

55
Time for action – adding a t
ext field to comments

55
Time for action – r
earranging the order of the Email field

58
Reusing e
xisting fields

59
Time for action – r
eusing the Notes field for the Recipe content type

59
Time for action – r
eusing the Source field for the Recipe content type

60
Time for action – r
earranging the order of the Notes field

61
Time for action – r
eusing the Email text field in the Recipe content type

62
Time for action – r
earranging the order of the Email field

62
Summary

64
Chapter 3: Multiple Value Fields

65
Enabling multiple values

65
Time for action – enabling multiple v
alues

66
Adding Boolean fields

72
Time for action – adding a Boolean field

72
Setting de
fault values

75
Time for action – se
tting default values

75
Adding list fields

78
Time for action – adding lis
t fields

78
Multiple value widg
ets

83
Time for action – changing the number of the v
alues

86
Time for action – checkbo
xes/radio buttons widget with multiple values

87
Summary

90
Table of Contents
[
iii
]
Chapter 4: Taxonomy

91
What is taxonomy?

91
Crea
ting vocabularies

93
Time for action – cr
eating the Cooking Tip Category vocabulary

94
Time for action – cr
eating the Recipe Category vocabulary

96
Adding terms t
o vocabularies

98
Time for action – adding t
erms to Cooking Tip Category

98
Adding term r
eferences to content types

102
Time for action – adding a t
erm reference field to the Cooking Tip content type

103
Cat
egorizing content using taxonomy

108
Time for action – c
ategorizing Cooking Tip Content using taxonomy

108
Adding fields to v
ocabularies

113
Time for action – adding a field t
o the Cooking Tip Category vocabulary

113
Summary

118
Chapter 5: File and Image Fields

119
Adding and configuring file fields to content types

120
Time for action – adding and c
onfiguring a file field to the Recipe content type

120
Using file fields

128
Time for action – using the file field

128
Adding image fields t
o content types

133
Time for action – adding an imag
e field to the Recipe content type

134
Configuring imag
e field settings

135
Time for action – c
onfiguring an image field for the Recipe content type

136
Crea
ting custom image styles

141
Time for action – cr
eating a custom image style

141
Summary

146
Chapter 6: Managing Field Display

147
Field display

148
Time for action – a fir
st look at the field display settings

148
Forma
tting field display in the Teaser view

153
Time for action – f
ormatting the Body field display in teaser view

153
Cust
om display settings

157
Time for action – enabling the Full c
ontent view mode in custom display settings

158
Forma
tting the image field display on Full content

160
Time for action – f
ormatting the image field display on Full content

160
Forma
tting the comment field display

163
Table of Contents
[
iv
]
Time for action – formatting the comment field display

163
Forma
tting the vocabulary field display

165
Time for action – f
ormatting the vocabulary field display

165
Summary

167
Chapter 7: Contributed Field Modules

169
Drupal modules

170
Contribut
ed modules

170
Finding modules

171
Time for action – finding c
ontributed field modules through the Drupal website

171
Time for action – finding c
ontributed field modules through the
drupalmodules.com website

173
Inst
alling and enabling contributed modules

176
Time for action – ins
talling a module

176
Time for action – ins
talling a module using Update manager

180
Contribut
ed field modules

184
Email field module

184
Link module

185
Table field module

185
Computed field module

185
Cont
ent Templates (Contemplate) module

185
Summary

185
Chapter 8: Field Level Permissions

187
Downloading the Field Permission module

188
Time for action – do
wnloading the Field Permission module

188
Inst
alling and enabling the Field Permission module

189
Time for action – ins
talling and enabling the Field Permission module

190
Enabling field-lev
el permissions

192
Time for action – enabling field-le
vel permissions

192
Configuring field-le
vel permissions on the PERMISSIONS administration page

195
Time for action – c
onfiguring field-level permissions on the PERMISSIONS
administration page

196
Field-lev
el permission reports

199
Time for action – the Field permissions r
eport page

199
Summary

202
Chapter 9: Theming Fields

203
What is a theme?

204
What is theming?

204
How Drupal r
enders a webpage

205
How t
o get started on theming

206
Crea
ting a Drupal theme

207
Table of Contents
[
v
]
Time for action – creating a Drupal theme by cloning the existing theme

207
Overriding t
emplate files

211
Time for action – cr
eating a template file for the Recipe content type

212
Editing t
emplate files

214
Time for action – editing a t
emplate file

214
Styling the page using CSS

221
Time for action – s
tyling the page using CSS

221
Summary

224
Chapter 10: A Database Perspective of Fields

225
Key elements in Fields

226
Entities

226
Bundles

226
Field st
orages

226
Field types

226
Fields

227
Field inst
ances

227
Field widge
ts

227
Field forma
tters

227
Fieldable entities

227
Ta
xonomy

228
New c
oncepts in Drupal 7

228
Unders
tanding nodes in Drupal

229
Turning e
verything into a node

229
Is ev
erything an entity?

230
Entities ar
e the new nodes?

230
Are the
y entities or objects?

231
A dat
abase perspective of fields

232
Cont
ent types

232
Time for action – cr
eating a new content type

234
Fields

238
Field-rela
ted database tables

240
Summary

250
Pop Quiz Answers

251
Chapter 1, Nodes and Content types in Drupal 7

251
Pop quiz – under
standing the default content types

251
Pop quiz – cr
eating content types

251
Pop quiz – adding c
ontent using custom content types

251
Table of Contents
[
vi
]
Chapter 2, Fields

252
Pop quiz – enabling the Field module

252
Pop quiz – finding the Field UI

252
Pop quiz – finding the field types and input widg
ets in Field UI

252
Pop quiz – adding t
ext fields using the text module

252
Pop quiz – r
earranging the order of fields

252
Pop quiz – adding t
ext fields to comments

252
Pop quiz – r
earranging the order of fields in comments

253
Pop quiz – r
eusing fields in content types

253
Pop quiz – r
eusing fields in content types

253
Chapt
er 3, Multiple Value Fields

253
Pop quiz – multiple v
alues in fields

253
Pop quiz – under
standing the Boolean field

253
Pop quiz – se
tting default values

253
Pop quiz – adding lis
t fields

254
Pop quiz – under
standing multiple value widgets

254
Chapt
er 4, Taxonomy

254
Pop quiz – cr
eating vocabularies

254
Pop quiz – adding t
erms to vocabularies

254
Pop quiz – adding t
erm references to content types

254
Pop quiz – c
ategorizing content using taxonomy

255
Pop quiz – adding fields t
o vocabularies

255
Chapt
er 5, File and Image Fields

255
Pop quiz – adding and c
onfiguring file field settings

255
Pop quiz – using the file field

255
Pop quiz – c
onfiguring image field settings

255
Pop quiz – cr
eating custom image styles

256
Chapt
er 6, Managing Field Display

256
Pop quiz – a fir
st look at the field display settings

256
Pop quiz – f
ormatting field display in the teaser

256
Pop quiz – cus
tom display settings

256
Pop quiz – f
ormatting image field display on Full content

256
Pop quiz – f
ormatting comment field display

257
Pop quiz – f
ormatting vocabulary field display

257
Chapt
er 7, Contributed Field Modules

257
Pop quiz – finding a c
ontributed field module

257
Pop quiz – ins
talling and enabling contributed modules

257
Table of Contents
[
vii
]
Chapter 8, Field Level Permissions

257
Pop quiz – ins
talling and enabling the Field Permission module

257
Pop quiz – enabling field-lev
el permissions

258
Pop quiz – c
onfiguring field-level permissions on the PERMISSIONS
administration page

258
Pop quiz – the Field permissions r
eport page

258
Chapt
er 9, Theming Fields

258
Pop quiz – cr
eating a Drupal theme by cloning the existing theme

258
Pop quiz – cr
eating a template file for the Recipe content type

258
Pop quiz – editing a t
emplate file

259
Pop quiz – s
tyling the page using CSS

259
Chapt
er 10, A Database Perspective of Fields

259
Pop quiz – k
ey elements in Fields

259
Pop quiz – ne
w concepts in Drupal 7

259
Pop quiz – cr
eating a new content type

260
Pop quiz – a da
tabase perspective of fields

260
Index

261
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
[
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.
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.
Preface
[
3
]
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
http://www.drupal.org
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
Preface
[
4
]
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:
<?php
// 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:
<?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 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
feedback@packtpub.com
, 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
suggest@packtpub.com
.
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
http://www.PacktPub.com
. If you purchased this book elsewhere, you
can visit
http://www.PacktPub.com/support
and register to have the files e-mailed
directly to you.
Preface
[
6
]
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
http://www.packtpub. 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
http://www.packtpub.com/support
.
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
copyright@packtpub.com
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
questions@packtpub.com
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 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 ex
ample project

‹ A pre
view of the final project

‹ What is a c
ontent type?

‹ Def
ault content types in Drupal 7 core

‹ Crea
ting content using article content types

‹ Crea
ting custom content types

‹ Adding con
tent using custom content types
Nodes and Content types in Drupal 7
[
8
]
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 t
o create custom web content by creating your own content types and fields

‹ How t
o create fields for displaying multiple values

‹ How t
o create taxonomy vocabularies and terms for categorizing web content

‹ How t
o use the file field to provide file uploading functionality to the website

‹ How t
o use the image field to provide image uploading, extension validation, and
image processing functionality to the website

‹ How t
o format images using image styles

‹ How t
o control the display of fields in teasers and full content view modes

‹ How t
o set field level permissions for specific fields that only authenticated users
can access

‹ Finally, ho
w 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.
Chapter 1
[
9
]
Nodes and Content types in Drupal 7
[
10
]
This is the homepage of the cooking website; it shows a list of the latest content uploaded
to the site. This website is composed of a mixture of content types like Article, Recipe, and
Cooking Tip. Each content type contains various field types and configurations. We will show
you how to create these content types in this chapter, and then we will create custom fields
in Chapter 2.
The preceding screenshot shows the Article content type which is one of the default content
types that comes with Drupal. This means that it has some fields already created for us such
as Title, Body, image, and Tag fields, by default. The screenshot shows the Article content
type in teaser view mode such as you would get in a listing format. Furthermore, as you can
see, the body text is positioned on the right side of the image. The default layout is body
text below the image, but we will use CSS to manipulate the teaser view mode layout to a
different position. We will guide you on how to do this by theming fields in Chapter 9.
The site administrators or authors (users with permissions to create article content) will use
this content type to create cooking articles for the website.
Chapter 1
[
11
]
The preceding screenshot depicts the Cooking Tip content type that is one of the custom
content types we will create in this book. The Cooking Tip content type contains the Title,
Image, Body, Tip Owner (Boolean field), and cooking tip Category (Term reference) fields.
We will see all these fields in detail later. We will also use the Field UI to manage the field
displays and change their order.
The preceding screenshot is of the Recipe content type. It is another custom content type
we are going to create. This content type contains more fields than the others. It will contain
Title, Body, Image, File, Difficulty (List: text), and recipe Category (Term reference) fields.
Nodes and Content types in Drupal 7
[
12
]
The Recipe content type is similar to the Cooking Tip content type, but it has a special field
type, which is list, and we will create the Difficulty field by using this field type to define the
difficulty level of a recipe.
The site administrators or authors can create a recipe using this content type, and indicate
the difficulty level of a recipe on the website.
Chapter 1
[
13
]
We will do some advanced theming for the Recipe content type to look like the preceding
screenshot. As you can see, we will change the body field position to the left of the page,
and position the Difficulty level and the Recipe Category (term reference field) to the right,
so people may find it easier to look at both information in different boxes at the center of
the page. If the body field has got a lot of information, it is more difficult for users to find
the difficulty level and the recipe category information. We will guide you on how to theme
fields in Chapter 9.
There is a file field created in the recipe content type, which allows the site administrators
or authors to upload PDF versions of recipes or other materials such as wallpapers
and coupons.
What is a content type?
Most of the time there is a need for more than one type of content on a website. We usually
see a newspaper website that contains news, pictures, videos, and forums sections. In terms
of Drupal, a node is a piece of content, and every piece of content belongs to a unique
content type, a content type that can contain various configurations and settings to define its
own composition and behavior.
Nodes and Content types in Drupal 7
[
14
]
It is similar to a pancake maker, we set up a pancake maker to make a red bean pancake.
Once we turn it on, it will keep making the same type of red bean pancake again and again.
It will not suddenly make us a strawberry pancake, unless we use other ingredients and set
up the pancake maker to create another type of pancake.
Content types /
Node types
Nodes/Contents
Title
Image
Body
Tag
Article
Title
Image
Body
Tag
Title
Image
Body
Tag
Title
Image
Body
Tag
Title
Image
Body
Tag
Default content types in Drupal 7 core
After installing Drupal, there are two default content types that come with Drupal out of the
box, they are the Basic page content type and the Article content type. We can use these
content types to create a simple website.
Chapter 1
[
15
]
The Basic page content type
The Basic page content type only contains the Title and Body fields. We can write plain text
and also insert HTML markup in the Body field.
Text format will finally determine what type of format the Body field will support
and display:
For example, if we install and enable the
PHP
filter module, it allows us to insert PHP code
in the Body field, which means we can write PHP code to connect to databases, and extract
data from the databases, and output the information on the website. There are many other
filter modules on the Drupal website such as
Markdown
filter module,
BBCode
filter module,
and so on. You can go to the website module download page to see more, and give them a
try; you may find one or two filter modules useful for your projects.
Nodes and Content types in Drupal 7
[
16
]
The Article content type
The Article content type is similar to the Basic page content type, except it allows us to
upload images and enter a set of tags to categorize the content.
The default content types are basically enough for a general purpose content managed
website. For example, we can use the default content types to create a blog website. The
Basic page content type can be used for creating About Us and Contact Us pages, and the
article content type can be used for creating blog posts.
Time for action – creating contents using
the article content type
Let's get started by creating some web content using the default content type. Our first task
is to add cooking articles to the cooking website using the Article content type.
1.

First log in to the website as an administrator, and go to the administration menu
a
t the top of the page, click on the Add content link to create new content:
Chapter 1
[
17
]
2.

It will show two content types on the page, one is Article, the other is Basic page.
Click on the Article link to add a cooking article to the site:
3.

You can type content in the fields, or copy and paste content from the final project
f
older if you have downloaded it. It contains a file called
content.txt
, you will
find all the text to input in this file. Also there is a directory called
images
, which
contains all the images for you to use when following along with the book. There
is a file called
salad.jpg
, you can use it for this tutorial.
Nodes and Content types in Drupal 7
[
18
]
4.

After filling in all the details, we leave the other settings to their defaults. Then go to
the bott
om of the page and click the Save button to save the content:
5.

Once you have saved the content, it will redirect you to the full content view
mode of the c
ontent, and it will show a message saying the content has been
successfully created:
Chapter 1
[
19
]
6.

If you go back to the homepage, you will see the content listed on the page as in the
f
ollowing screenshot.
What just happened?
We have just added content using the Article content type. We entered the Title, the Body
text, and we also attached an image to it.
Pop quiz – understanding of the default content types
1.

Which of the following types are the default content types in Drupal 7?
a.

Pag
e and Image content type
b.

Basic page and P
age content type
c.

Article and Basic page c
ontent type
d.

Article and Pag
e content type
2.

To which de
fault content type can we attach images?
a.

Image c
ontent type
b.

Article con
tent type
c.

Basic page c
ontent type
Nodes and Content types in Drupal 7
[
20
]
Creating custom content types
This time we will be creating two custom content types, which are called the Cooking Tip
and the Recipe content type. We will mainly use these custom content types to create our
cooking website.
Creating the Cooking Tip content type
The Cooking Tip content type will contain all the cooking tips information on the website.
Users can navigate around the cooking tips to learn and share tips with other people.
Time for action – creating the Cooking Tip content types
Let's create the Cooking Tip content type now. Follow these steps:
1.

Go to the administration menu bar at the top of the page. We will see a Structure
link be
tween the Content and Appearance, just click on it.

Chapter 1
[
21
]
2.

A page will show up with the following options. We can ignore the other options
f
or now. Let's just focus on creating our custom content types. Click Content types,
which is below the Blocks link and above the Menus link on the page:
3.

It will go to the Content types administration page, and this page will show all
the e
xisting content types in your Drupal site. Currently it only shows the default
content types:
Nodes and Content types in Drupal 7
[
22
]
4.

Click the Add content type link:
5.

On this page we will enter our content type information such as the name of the
content type, a description of the content type, and other specific settings:
Chapter 1
[
23
]
6.

We enter Cooking Tip as the name of the content type, and enter a sentence to
describe the content type, and then we just leave the other setting as default. Click
the Save content type button at the bottom of the page.
7.

The Save and add fields button is also very useful, if we want to start adding more
fields after saving the content type. But we will not use it for now; we will add more
fields in the next chapter.
Nodes and Content types in Drupal 7
[
24
]
8.

After clicking the Save content type button, it will redirect you back to the Content
types administration page with a message saying that the content type has been
successfully added to Drupal:
9.

Now we can see our recently added content type Cooking Tip in the list on the
Content types administration page.
What just happened?
Now the Cooking Tip content type is created. We will use the same procedure to create the
Recipe content type.
Creating the Recipe content type
The Recipe content type will contain the ingredients used in the recipe, the instructions, the
difficulty of the recipe, and we can also define a set of terms for the content.
Chapter 1
[
25
]
Time for action – creating the Recipe content type
Now we will create the Recipe content type, and we will create fields in the later chapters to
enhance the functionality of this content type. Follow these steps to create this content type:
1.

In the Content types administration page, click the Add content type link:
2.

Now you can enter Recipe as the name of the content type, similar to the Cooking
Tip c
ontent type. You can enter a sentence to describe this content type.
Nodes and Content types in Drupal 7
[
26
]
3.

When you are done, like the last exercise, go to the bottom of the page, and click on
the
Save content type button to save this content type in Drupal.
4.

If we go back to the Content types administration page, you can check the existing
content types in Drupal, just like the following screenshot. It shows that we have
a total of four content types, namely, two custom content types and two default
content types:
What just happened?
We have created the Cooking Tip content type and the Recipe content type. All the content
types are created, the foundation of the website is built, we will use these content types for
the rest of the cooking website.
Pop quiz – creating content types
1.

Can we change the name of the content type after creating it?
a.

Yes
b.

No
2.

Which of the follo
wing statements is true?
a.

We c
an create a content type with the same name as another existing
content type.
b.

We c
an recreate default content types if we deleted any of the default
content types.
Chapter 1
[
27
]
Adding content using custom content types
We have already created two custom content types. We will add content using these content
types. You will find that adding content using the custom content types does not differ
greatly from adding content using the default content types. It is only when we start creating
more fields in the custom content types that you will gradually understand the differences
and characteristics of each content type.
Adding content using the Cooking Tip content type
In this section, we will add content using the Cooking Tip content type.
Time for action – adding content using
the Cooking Tip content type
Let's get started by adding some content to the website using our custom content types.
Follow these steps to add content using Cooking Tip:
1.

Go to the administration menu bar at the top of the page, and click Add content:
Nodes and Content types in Drupal 7
[
28
]
2.

You will see that Cooking Tip and Recipe are added on the Add new content page
because we have created those content types previously. Click the Cooking Tip link:
3.

It is similar to the Basic page content type, which only contains the Title and Body
fields. W
e will add more fields later. We can start entering text in the Title and the
Body fields, but for your convenience, you can also copy and paste the text from the
content.txt
file if you have downloaded the final project files:
Chapter 1
[
29
]
4.

Then we click the Save button at the bottom of the page, and just leave other
se
ttings as default:
5.

Once the content is saved, it will redirect you to the full content view mode of the
content you just created. There are two tabs at the top of the content, which are the
View and the Edit tabs. If we want to edit or update the content, we can click the
Edit tab. It will show you the edit page of the content, and we can start to do any
modification on that page:
Nodes and Content types in Drupal 7
[
30
]
What just happened?
We have added content using the Cooking Tip content type to the cooking website. We
entered the title of the cooking tip, and we also entered the cooking tip content to the
website. Visitors can now read our cooking tip on the website.
Adding content using the Recipe content type
Now we should understand the workflow of how to add content using the custom content
types. This time we will add content using the Recipe content type, this shouldn't be a
problem for us, and we should be able to do it quicker now.
Time for action – adding contents using the Recipe content type
In this section, we will add content using the Recipe content type.
1.

Go to the administration menu bar at the top of the page, and click Add content.
2.

Click the Recipe link on the Add new content page.
3.

On the Create Recipe page, enter text in the Title and the Body fields or copy and
paste the text from the
content.txt
file:
Chapter 1
[
31
]
4.

Click the Save button at the bottom of the page to save the content. It will show the
full vie
w mode of the content as in the following screenshot:
What just happened?
We have added content using the Recipe content type to the cooking website. We have
entered the ingredients and the cooking method of the cake recipe, and now, users can make
a cake by following the recipe you just posted.
As we have seen so far, adding content using the Recipe custom content type is not much
different from adding content using the Cooking Tip content type. However, in later chapters,
we will learn and add a variety of fields to these different content types, and you will
understand more about the real power of fields and the relationship between fields and
content types.
Nodes and Content types in Drupal 7
[
32
]
Pop quiz – adding content using custom content types
1.

Can we clone an existing node/content using default Drupal functionality?
a.

Yes
b.

No
2.

Can we chang
e the content type of a node/content to different content type?
a.

Yes
b.

No
Have a go hero – creating more content using the custom content types
Now it is time to test your skills in creating more content using the custom content types.
You can try to enter more content with the methods we have learned. Also there are options
at the bottom of the page when creating content. You can try to change those settings, and
see what they give you.
Try to do all the steps on your own without looking at the chapter first. If you encounter any
difficulties, read the chapter again to get familiar with the steps.
Summary
In this chapter, we have dealt with the following key topics:

‹ A pre
view of the complete project in this book. We have an overview of the
structure of the final project, and we have an idea of what will be achieved
when we reach the end of this book.

‹ What a node and a c
ontent type is, and the meaning and the relationship between
those terms in Drupal.

‹ Unders
tanding what default content types are in Drupal 7 core and what are the
fields created in those content types by default.

‹ How t
o add content using the default content types.

‹ Unders
tanding the workflow of creating custom content types.

‹ Adding con
tent using custom content types.
Now that we've learned how to create content types, we're ready to learn how to create
fields and add them to the custom content types. This is the main purpose of fields, and
we will discover the power of fields, which is the topic of the next chapter.
2
Fields
In this chapter we will learn the concepts needed to work with the fields
available in Drupal. It also gives a brief explanation of what the fields set of
modules are designed for and what has changed since the arrival of the new
and improved Drupal 7. This chapter also guides you on how to add fields
to content types, rearranging the order of fields, reusing existing fields, and
adding fields to comments.
The topics that will be discussed in this chapter are:

‹ What is CCK?

‹ Fields in Drupal 7

‹ Drupal 7 cor
e default fields

‹ Adding fields to c
ontent types

‹ Rearr
anging the order of fields

‹ Adding fields to c
omments

‹ Reusing e
xisting fields
What is CCK?
So what is CCK? Content Construction Kit (CCK) is a set of modules developed for Drupal
which allows us to add custom fields to any kind of content type including core content
types and custom content types, all through a web interface.
Fields
[
34
]
But when and why do we need it? Unless the functionality provided by the default Article
and Basic page content types is all you need for your website you'll need a way to define
a mold or more for the content on your website. This is what CCK does for you. It lets you
define the composition and behavior of you site's content.
As we have learned so far, we can create custom content types to solve a specific problem,
but content types by default contain exactly the same number of fields. The only difference
between them is the name of content types. We need a solution that allows us to add more
fields and functionalities to content types, so that each content type is responsible for
different jobs.
The functionality provided by custom fields varies greatly. Some of the more useful ones
allow users to upload images, display them on the page, or upload videos so users can share
their travel videos with friends. The CCK set of modules can enrich a simple website to be a
social media website, sharing photos, and sharing ideas between different groups of people
on the Internet.
Our clients require that the website allows users the ability to submit cooking tips and
recipes to the website. They require that cooking tips and recipes have different, custom
functionalities like displaying food pictures and setting difficulty levels for recipes. The
default content types cannot satisfy such requirements.
So, in the previous chapter, we tried to fulfill the client requirements by creating two custom
content types. However, if you have noticed the custom content types we created have the
exact same structure. They both contain the Title and the Body field and, as such, those
fields cannot do very much other than allowing users to enter the title and the body details.
The following image shows the Cooking Tip content type fields:
The following image shows the Recipe content type fields:
Chapter 2
[
35
]
They cannot be used to set the difficulty level of recipes or for displaying images. We need
more fields to satisfy these requirements, so we will add more custom fields using the Field
module in this chapter.
Fields in Drupal 7
In Drupal 6, the CCK module is a contributed module; we have to download the CCK module
from
http://drupal.org/project/cck
, and add it to the
sites/all/modules

directory, and then go to the
ourwebsite.com/admin/build/modules
page to install
the module.
The good news is that most of the functionality of the CCK modules is available in Drupal
7 core, and now it is simply called Fields. From now on, we do not need to download a
separate module to add fields to content types. We simply install Drupal 7 and the ability
to create custom fields is enabled as part of it.
As a result of the name change, to avoid any confusion in the later
chapters, we will call the CCK module or CCK Fields to the field module.
For example, the instruction will be "we will use the Field module to
add a text field to the recipe content type", but not "we will use the CCK
module to add a text field to the recipe content type".
Enabling the Field module
When we installed Drupal 7, the Field module is installed and enabled by default. If you want
to make sure the Field module is installed and enabled, we can carry out the following steps.
Time for action – enabling the Field module
Let's enable the Field module in this section. Follow these steps:
1.

We login as the site administrator. Go to the administration toolbar at the top of the
pag
e, and click the Modules link to go to the module administration page:
Fields
[
36
]
2.

We will find the Field module when we scroll down the page as shown in the
following screenshot. You will also see the page which not only contains the Field
module, but also two more field related modules, one is called Field UI and the
other Field SQL storage. We have to also make sure these modules are installed
and enabled; otherwise, Fields will not function correctly in Drupal.
3.

If you cannot find the Field module on the module administration page or it is
disabled,
you have to make sure you have downloaded the correct version of Drupal,
and double check the downloaded file has no file corruption. If you find your Drupal
7 installation file has got errors, you should download the Drupal 7 installation file
from
http://drupal.org/project/drupal
, and reinstall Drupal.
Chapter 2
[
37
]
What just happened?
We learned how to view the module administration page to verify that the Field modules are
installed and enabled.
Pop quiz – enabling the Field module
1.

Which of the following statements is true?
a.

The field module is enabled by de
fault.
b.

The field module is not inst
alled by default when installing Drupal.
Field, Field UI, and Field SQL storage modules
The Field module is an Application Programming Interface (API) for Drupal developers to
develop custom fields that can be embedded to Drupal entities such as nodes, comments,
users, and terms. The Field module will do all the heavy lifting work for you to store, load,
edit, and render field data, so we don't have to develop each individual function to create
custom fields.
What is an API?
API stands for "Application Programming Interface" and is a set of tools usually in
the form of code libraries aiding in the development of software programs. You
can find out more information from:
http://en.wikipedia.org/wiki/Application_programming_
interface
However, site builders usually do not create custom fields through the Field API directly,
because we have to develop Drupal modules, in order to use the Field API to create Drupal
entities and to add fields to Drupal entities.
Instead, we will use the Field UI module to create custom fields, this module provides us with
user-friendly interfaces that allow us to interact with the Field API easily. So we can create
and configure custom fields without any programing knowledge, through a web interface,
the Field UI module will automatically create custom fields for us.
The Field SQL storage module is an API that performs database operations for the Field API.
This module is like the Field API in the sense that it has no user interfaces; this module is
intended for managing internal field data operations. This is an advanced topic, so the Field
SQL storage API will not be covered in this book.
Fields
[
38
]
Time for action – finding the Field UI
If you need to find out where the web interface the Field UI has provided for us is located
inside our website, we can perform the following steps:
1.

Click the Structure link on the administration toolbar at the top of the page:
2.

Click the Content types link on the structure administration page. It will direct us to
the c
ontent types administration page.
3.

There are two columns in the table as you see in the following screenshot, the first
c
olumn is the names of the content types on your website, the second column is the
operations we can perform on the content types.
Chapter 2
[
39
]
4.

There are two options provided by the Field UI module, which are manage fields
and manag
e display in the operations column. If you cannot see the links on the
page, it means that the Field UI module is disabled or problems have come from a
possibly corrupted Drupal 7 installation file.
What just happened?
We have identified the Field UI options on the Content type administration page.
There are two options we have found, one is manage fields, and the other is manage
display. Once the Field module is enabled, these two options will be available on the
content types administration page.
Pop quiz – finding the Field UI
1.

Which of the following statement is true?
a.

The manag
e fields link is on the Add new content administration page.
b.

The manag
e fields link is on the Content type administration page.
c.

The manag
e display link in on the Appearance administration page.
Fields
[
40
]
Field types and input widgets
The Field module provides the foundation for fields; extra modules are needed for field types
and input widgets. Some of the field types and input widgets are available in the core, such
as Number, Text, and List modules.
Time for action – finding the field types and
input widgets in Field UI
But where can I find the field types and input widgets in field UI? To find them you will need
to carry out the following steps:
1.

Navigate to the content types administration page, and click one of the manage
fields
links as shown in the following screenshot:
2.

There is a table of five columns on the page. The first column represents the labels
of the fields, the sec
ond column is the names of the fields, the third columns is the
field types of the fields, the fourth column is the input widgets of the fields, and the
last column is the operations for the fields. The manage fields page provides all the
information about each field for a content type, and we can also check which field
types and input widgets have been applied to the fields.
Chapter 2
[
41
]
3.

If you need more field types and input widgets, there are other contributed modules
tha
t are available from
http://drupal.org/project/modules
and we can
search the CCK/field related modules using the filters provided on that page, we can
select CCK in the Modules categories drop-down menu, and select 7.x in the Filter
by compatibility drop-down menu. It will provide a list of field related modules that
provide you more features and functionalities for fields:
What just happened?
We have identified the field types and input widgets on the manage fields page through the
Field UI. The manage fields page shows the available field types and input widgets on the
table. We can easily identify what field is used for what type of field types and input widgets.
We can obtain more field types and input widgets from the Drupal.org website, they provide
a variety of fields related modules to extend the functionality of field modules.
Fields
[
42
]
Pop quiz – finding the field types and input widgets in Field UI
1.

In which page can we find out what fields are used in a content type?
a.

Manage fields pag
e
b.

Cont
ent types administration page
2.

Are Field types and input widg
ets extensible?
a.

Yes
b.

No
Drupal 7 core default fields
There is a list of field related modules that are part of Drupal 7 core. Some of these modules
are required by the Field module and Drupal, but some of them are optional. We can easily
disable them from the modules administration page. Furthermore, not all of these modules
provide both field types and input widgets for fields, some of them may only provide you
with field types and no additional input widgets. And some of them will only provide you
with input widgets without giving you a new field type. If you download contributed modules
from drupal.org, it would be better to read the module documentation to understand what
the modules can do for you.
Drupal 7 core field related modules
Drupal 7 core includes the following field-related modules:

‹ Number (requir
ed)

‹ Te
xt (required)

‹ List (r
equired)

‹ Ta
xonomy (optional)

‹ Image (op
tional)

‹ File (optional)
Number modules
The number module provides different numeric field types for fields such as Integer,
Decimal, and Float formats:
Chapter 2
[
43
]
Text modules
The text module provides different text field types for fields such as Text, (single row), Long
text, and Long text with summary fields (multiple rows):
Fields
[
44
]
List modules
The list module provides a facility to store a list of items for fields, and often these items are
inputted from select lists, checkboxes, and radio button input widgets:
Taxonomy modules
The taxonomy module provides a facility to categorize content on a website, and the
taxonomy term fields can be attached to content types for users to categorize the content.
Chapter 2
[
45
]
Image modules
The image module provides a facility that allows us to attach images to fields.
File modules
The file module provides a facility that allows us to attach files to a field.
Fields
[
46
]
Adding fields to content types
There are usually cases in which we need to add a field to content types; site administrators
can enter data to the custom fields for a specific purpose.
Now, we go back to our cooking website, we will add more custom fields to the Recipe
content type and the Cooking Tip content type, because our clients want to have a field to
enter notes for the recipes and the cooking tips. Additionally they would like to have another
field to enter the source of recipes and cooking tips, so authors can enter where they found
the recipes or tips.
Adding text fields using the text module
In this case, the text module can perfectly handle this requirement. Authors can enter text
into a text area field that contains a note for a recipe or a cooking tip. We can also create a
text field, and authors can enter details for the source of a recipe and a cooking tip.
Time for action – adding Long text fields using the text module
Text areas allow users to enter more than one line of text, which can be used for news
postings and user enquiries. In this section, we will add a Long text field using the text
module. Follow these steps:
1.

Navigate to the Content types administration page:
Chapter 2
[
47
]
2.

Click the manage fields link on the Cooking Tip row as shown in the following
screenshot. We will only manage the fields for the Cooking Tip content type for now.
We cannot manage fields for multiple content types at the same time, we have to do
it one by one.
3.

We can enter "Notes" for the field label. Then enter "notes" for the field name
(y
ou can only use numbers, lower-case letters, and the underscore character for
the field name).
4.

Select Long text from the Select a field type drop-down menu in the FIELD column.
After you select the field type, the Text area widget will be automatically selected as
the input widget. Because the Long text field type is only supported by the text area
input widget, no other widget can be selected.
Fields
[
48
]
5.

Click Save to save the field.
6.

After clicking the Save button, the following message is displayed:
These
settings apply to the Notes field everywhere it is used. These settings impact
the way that data is stored in the database and cannot be changed once data has
been created.
Which means, all the settings here will be used for the Notes field of the Cooking
Tip content type in Drupal, and all the data created by this field that will be saved in
the database. However, once the data were created and saved in the database, you
cannot change the saved data in the database by modifying the field settings.
7.

Click the Save field settings button to confirm the message, and we will start to
c
onfigure the field on the field setting page.
8.

On this page, the Label field is automatically filled in based on the label name we
entered earlier; we can change it in here if we need.
9.

We will not check the Required field checkbox because we want the Notes field
to be optional.
10.

Rows defines how many rows the text area will be; we just leave it as the default
v
alue (5).
Chapter 2
[
49
]
11.

There are two options for Text processing, one is Plain text, and the other is Filtered
text (user selects text format).
12.

The Help text text area allows us to enter some tips and description to help users
under
stand more about what this field is, and what information users should enter.
In this case, we can enter Enter notes to the cooking tip for this field.
13.

For now, we can leave the DEFAULT VALUE field, and proceed to the next field. We
use the
DEFAULT VALUE field in a later chapter.
Fields
[
50
]
14.

We also leave the Number of values as 1, so we don't change anything here, we just
click the Save settings button on the field settings:
Drupal will direct us back to the Manage fields page. As we can see on the page, the Notes
field is added below the Body field in the table, and we can see it shows what field type and
input widget it is using. If we would like to go back and edit the notes field settings, we can
easily just click the edit link to edit the field settings. We can even delete the field by clicking
the delete link, but you have to make sure the field is not needed, because there is no undo
button to recover your deleted fields, so use it wisely and carefully:
What just happened?
We have successfully created a Long text field by using the text module, and users will use it
to enter the note of cooking tips.
Text fields
This time we will create a field using the text field type, and authors can enter a short
paragraph for the source of cooking tips. We just learned how to add a field to content types,
and the text area field type is similar to the text field type. We should able to create it easily.
Chapter 2
[
51
]
Time for action – adding text fields using the text module
Text fields allow users to enter a line of text, which can be used for users entering username
or postal numbers. In this section, we will create a text field using the text module. Follow
these steps:
1.

Type "Source" in the Label field, and then type "source" in the Field name field, and
select
text from the Select a field type drop-down menu in the FIELD column. The
input widget will be automatically changed to a Text field widget. This is just like the
text area field type, there is only a text field widget that supports the text field type,
so the text field widget will be selected by default.
2.

Then click Save to save the field.
3.

Next, it will display a page with a message that is similar to the one we have seen
be
fore. All data cannot be changed, once the data is saved in the database. But the
bottom part, which is a little different from the text area field type, here provides
the Maximum length option, so we can set the maximum length of the field, and
this option only applies to the Text field field type. We leave the default value, and
click the Save field settings button.
Fields
[
52
]
4.

This page, as you can see, is similar to the text area field setting page, so the same
se
ttings will not be covered again, because we already know what they do.
5.

Now we enter "Enter the source of cooking tips" into the Help text field:
6.

Click the Save settings button to save the field settings.
7.

In the Manage fields page, the source field has been successfully added to the
Cooking Tip content type now.
Now, we know how to create Long text and Text field fields to the Cooking Tip content type.
There are many cases where we need to add our own custom Text area and Text field to
content types, but from now on, this is not a question for us anymore, because we know
how to create them easily through the Field UI.
Chapter 2
[
53
]
What just happened?
We have created a text field by using the text module. Users will use it to enter the source of
cooking tips.
Pop quiz – adding text fields using the text module
1.

How many types of fields can the text module create?
a.

1
b.

2
c.

3
2.

Which type of field does the te
xt module can support?
a.

Image field
b.

Long te
xt field
c.

List field
Rearranging the order of fields
So far we have seen how to create fields to content types. The latest created fields will be
appended to the existing fields, so the latest created fields will be positioned at the bottom
of the page. But what if we want the Source field to sit at the top of the page, rather than
sitting at the bottom of the page. The Field UI provides a user-friendly interface that lets us
easily rearrange the order of each field.
Rearranging the order of the Notes field
Our client wants the Notes field to be positioned just below the Title field, because they
want to check the notes quicker. They don't want to scroll down the page to check the notes.
Time for action – rearranging the order of the Notes field
In this section, we will rearrange the order of the Notes field. Follow these steps:
1.

Navigate to the Content types page, in the table, locate the row that contains the
Cooking Tip c
ontent type, and click the manage fields link.
Fields
[
54
]
2.

There are four-headed arrow icons on the left of each row in the table. We locate
the
Notes field, and then drag the four-headed arrow icon up to the position that
as just below the Title field.
3.

After dragging it, we should see the highlights and the message as in the following
scr
eenshot:
4.

Click the Save button at the bottom of the page. (After changing the order, you
should sa
ve the settings otherwise the order will not be saved.)
5.

The highlighted message is gone after saving the page, and the Notes field is now
positioned belo
w the Title field:
Chapter 2
[
55
]
What just happened?
We just rearranged the order of the Notes field.
As you saw, rearranging the order of fields is so easy to do using the Field UI, by dragging and
dropping, any field on this page can be rearranged. Also we saw the highlighted message
when we dragged the Notes field. Actually, it just warns us that we have just made a change
to the fields in the table, but it will not take effect until we click the Save button to save
the change.
Rearranging the order of fields affects the node/content forms, full content, and teaser mode
outputs. When you add or edit a Cooking Tip content, you will see the order of the Notes
field has now moved below the Title field, and the Node field has also changed its position
on the full content page.
Pop quiz – rearranging the order of fields
1.

Does the reordering of fields affect the page output?
a.

Yes
b.

No
Adding fields to comments
Drupal 7 allows us to add fields to comments; it was not possible to do this in Drupal 6
and earlier versions. Though you can write a custom module, by default Drupal 6 doesn't
allow adding of fields in the comments form. In the cooking website, our client wants to
add a text field that contains commenter e-mail addresses when they post a comment on
the website.
Adding text fields to comments
We just learnt how to add a text field to the Cooking Tip content type. In this section, we will
create a text field and attach it to the comments form.
Time for action – adding a text field to comments
Now we will add a text field to comments. Follow these steps:
1.

Navigate to the Content types page, in the table, locate the row that contains the
Cooking Tip c
ontent type, and click the manage fields link.
Fields
[
56
]
2.

Click the COMMENT FIELDS tab. It is positioned between the MANAGE DISPLAY and
COMMENT DISPLAY tabs:
3.

In the Add new field row in the table, enter "Email" for the Label field, enter "email"
for the Field name field, and then select Text for the field type, the Text field widget
will be automatically selected.
4.

Click the Save button.
5.

Click the Save field settings button. We leave the settings as default:
Chapter 2
[
57
]
6.

In the following page, we also leave all the settings as default, and then click the
Sa
ve settings button to save the field settings:
7.

We should get something like the following screenshot:
What just happened?
We have added a Text field to comments. Commenters can use this field to enter their e-mail
address when submitting comments.
Fields
[
58
]
Pop quiz – adding text fields to comments
1.

Can we add Text area fields to comments?
a.

Yes
b.

No
Time for action – rearranging the order of the Email field
Our client prefers the Email field to be positioned below the Author field. We just learnt how
to do this in the earlier section; we should replicate the same thing here easily:
1.

Locate the Email field, and then drag the four-headed arrow icon up to the position
that is just below the Author field.
2.

Then click the Save button.
3.

The result should look like the following screenshot:
What just happened?
We have rearranged the order of the Email field for comments.
Pop quiz – rearranging the order of fields in comments
1.

Can we reorder any field in comments?
a.

Yes
b.

No
Chapter 2
[
59
]
Reusing existing fields
So far we have created fields and applied the changes to the Cooking Tip content type, but
what about the Recipe content type. The Recipe content type also needs the Notes field,
the Source field, and the Email field. We can go through all the steps to create the same
fields, but there is a time saving feature in the Field module. We can reuse the existing fields,
without the need to recreate new fields to mimic the defined fields in other content types.
Time for action – reusing the Notes field for
the Recipe content type
In this section, we will reuse the Note field for the Recipe content type. Follow these steps:
1.

Navigate to the Content types page. In the table, locate the row that contains the
R
ecipe content type, and click the manage fields link.
2.

In the Add existing field row in the table, select Long text: field_notes (Notes) from
the drop-down menu, the Text area widget will be automatically selected, and then
enter "Notes" for the Label name.
3.

Click the Save button to save the change.
4.

The Field settings page is exactly the same as the one we saw when creating the
Notes field for the Cooking Tip content type.
5.

Click the Save settings button.
Fields
[
60
]
6.

On the Manage fields page, the Notes field is now added to the Recipe
content type.
What just happened?
We have just reused the Notes field for the Recipe content type.
Pop quiz – reusing fields in content types
1.

Can we reuse the fields created in different content types?
a.

Yes
b.

No
Time for action – reusing the Source field
for the Recipe content type
This time we will reuse the Source field for the Recipe content type. Follow these steps:
1.

In the Add existing field row in the table, select Text: field_source (Source) from the
drop-down menu. The Text field widget will be automatically selected. Then enter
"Source" in the Label field.
Chapter 2
[
61
]
2.

Click the Save button to save changes.
3.

On the Field settings page, leave all the settings as default, just click the Save
settings button to proceed to the next step.
4.

The final result should look like the following screenshot:
What just happened?
We have reused the Source field for the Recipe content type.
Time for action – rearranging the order of the Notes field
We have rearranged the order of the Notes field for the Cooking Tip content type. We will
do the same thing here for the Recipe content type. Follow these steps:
1.

Locate the Notes field, and then drag the four-headed arrow icon up to the position
tha
t is just below the Title field.
2.

Click Save.
Fields
[
62
]
What just happened?
We have rearranged the order of the Notes field in the Cooking Tip content type.
Time for action – reusing the Email text field
in the Recipe content type
Not only can fields created in content types be reused, but fields created in comments can
also be reused. We will reuse the Email text field, which we created earlier for the Recipe
content type. Follow these steps:
1.

Navigate to the Content types page, in the table, locate the row that contains the
R
ecipe content type, and click the manage fields link.
2.

Click on the Comment Fields tab.
3.

In the Add existing field row in the table, select Text: field_email (Email) from the
drop-down menu, the Text field widget will be automatically selected. Then enter
"Email" as the label name.
4.

Click the Save button to save the changes.
5.

In the Field settings page, click Save settings.
What just happened?
We have reused the Email text field in comments to the Recipe content type. Fields created
in comments can be reused in content types.
Time for action – rearranging the order of the Email field
We reused the Email text field for the Recipe content type, which needs to be reordered to
make it more convenient for users to enter their details. Follow these steps:
1.

Locate the Email field, and then drag the four-headed arrow icon up to the position
that is just below the Author field.
Chapter 2
[
63
]
2.

Then click Save. The result should look like the following screenshot:
What just happened?
We have rearranged the order of the Email text field in the Recipe content type.
Once we have created fields in Drupal, we can reuse all the existing fields for any kind of
Drupal entities such as nodes, comments, users, and terms. Therefore, if we created a field in
a content type, we can also reuse it in comments, or vice versa. The architecture of fields has
been completely rewritten, that is why we can use such a power feature in Drupal 7, and it
was very hard to achieve in Drupal 6 and earlier versions.
Pop quiz – reusing fields in content types
1.

Which of the following statements is not true?
a.

Fields crea
ted in content types can be reused in comments.
b.

We c
an reorder fields in comments.
c.

Fields crea
ted in comments can't be reused in content types.
Have a go hero – creating fields for content types and comments
We have learned a lot. It is now time for a challenge we can use the same methods we
have learned to create a field called an Extra Note for the Cooking Tip content type, so site
owners can use it to write down some extra notes for the cooking tip. Also, you can try to
create a Website text field for the cooking tip and recipe comments. The commenters can
leave their website URLs in their comments.
Fields
[
64
]
Summary
Now that you have finished this chapter, you should understand what CCK is and what a field
is in the Fields module in Drupal 7. We also understand what fields and what field types the
Drupal 7 core provides. By adding custom fields to content types and comments, we can add
more variation and features to contents. Furthermore, we have also learnt how easily we can
rearrange the order of fields, and reuse the existing fields for content types.
In the next chapter, we will learn how to create multiple value fields for content types. We
can create radio buttons, checkboxes, and select lists by using multiple value fields, and this
type of field can be used for educational websites such as quizzes and online exams, which
is the topic of the next chapter.
3
Multiple Value Fields
In the previous chapter, we discussed all the necessities for creating long text
and text field fields using the text module. In this chapter, we will cover multiple
value fields and multiple value widgets in Drupal 7, which allow a field to have
multiple values. Also, the interaction between the fields and the widgets when
using the multiple value options will be covered in detail in this chapter.
In this chapter, we shall learn:

‹ Enabling multiple values

‹ Adding Boolean fields

‹ Setting de
fault values

‹ Adding list fields

‹ Multiple values widg
ets
Enabling multiple values
In the previous chapter, we created a source text field to store the source information for a
cooking tip. However, there are times we need to enter more than one referenced source.
It may be the case that we've put together a tip using three or four different websites, but,
we cannot reference all of them because there is only one text field available, and also there
is no option to create and enter more fields. Next, we will learn how to enable the multiple
value option for the source field.
Multiple Value Fields
[
66
]
Time for action – enabling multiple values
In this section, we will enable the multiple value option for the Source field in the Cooking
Tip content type. Follow these steps:
1.

First, we have to login as an administrator—if you are following the book, you are
alr
eady logged in as an administrator.
2.
Click the Structure link on the administration menu at the top of the page:
3.

Click the Content types link on the next screen:
4.
Click the manage fields link to go to the Manage fields administration page:
5.
We would like to edit the Source field, not the other fields. So we click the edit link,
which is located at the Source row in the table:
Chapter 3
[
67
]
6.
Now we are on the Source field administration page, there are options we already
filled up in the previous chapter. But we will pay attention to the bottom of the