Drupal 7 FieldsCCK beginner's guide (2011) - Pdf 42


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 transmied in any form or by any means, without the prior wrien permission of the
publisher, except in the case of brief quotaons embedded in crical arcles or reviews.
Every eort has been made in the preparaon of this book to ensure the accuracy of the
informaon presented. However, the informaon 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 informaon about all of the
companies and products menoned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this informaon.
First published: July 2011
Producon 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 Mulmedia Studies and a Master's degree in IT. His love aair with Drupal began
aerwards, 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
prinng of this book) for her endless paence and support. Without her,
what I do would be meaningless.

I would also like to thank my father for his connued encouragement.

It is my great fortune to have worked with a great team of publishing
professionals at Packt Publishing. I extend my sincerest gratude to Usha
Iyer, Neha Mallik, Vanjeet D'souza, Neha Shey, and Michelle Quadros for
their cooperaon and help in wring this book.
About the Reviewers
Maurice Green
(PhD) began his love aair with computers in 1963 as a graduate student
in biochemistry using Fortran and punch cards. Rering aer a 40 year career as a medical
laboratory director specializing in automated laboratory instrumentaon and informaon
systems and an IT manager in the Department of Medicine at Stanford University, he turned
his aenon to photography, video eding, 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 paence.
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 oers 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 entled 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 collecon of free technical arcles, sign up
for a range of free newsleers and receive exclusive discounts and oers on Packt books
and eBooks.

Do you need instant soluons to your IT quesons? PacktLib is Packts online digital book
library. Here, you can access, read and search across Packt's enre 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 acon – enabling the Field module 35
Field, Field UI, and Field SQL storage modules 37
Time for acon – nding the Field UI 38
Field types and input widgets 40
Table of Contents
[
ii
]
Time for acon – 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 acon – adding Long text elds using the text module 46
Text elds 50
Time for acon – adding text elds using the text module 51
Rearranging the order of elds 53
Rearranging the order of the Notes eld 53
Time for acon – rearranging the order of the Notes eld 53
Adding elds to comments 55
Adding text elds to comments 55
Time for acon – adding a text eld to comments 55
Time for acon – rearranging the order of the Email eld 58
Reusing exisng elds 59

Adding term references to content types 102
Time for acon – adding a term reference eld to the Cooking Tip content type 103
Categorizing content using taxonomy 108
Time for acon – categorizing Cooking Tip Content using taxonomy 108
Adding elds to vocabularies 113
Time for acon – adding a eld to the Cooking Tip Category vocabulary 113
Summary 118
Chapter 5: File and Image Fields 119
Adding and conguring le elds to content types 120
Time for acon – adding and conguring a le eld to the Recipe content type 120
Using le elds 128
Time for acon – using the le eld 128
Adding image elds to content types 133
Time for acon – adding an image eld to the Recipe content type 134
Conguring image eld sengs 135
Time for acon – conguring an image eld for the Recipe content type 136
Creang custom image styles 141
Time for acon – creang a custom image style 141
Summary 146
Chapter 6: Managing Field Display 147
Field display 148
Time for acon – a rst look at the eld display sengs 148
Formang eld display in the Teaser view 153
Time for acon – formang the Body eld display in teaser view 153
Custom display sengs 157
Time for acon – enabling the Full content view mode in custom display sengs 158
Formang the image eld display on Full content 160
Time for acon – formang the image eld display on Full content 160
Formang the comment eld display 163
Table of Contents

Time for acon – enabling eld-level permissions 192
Conguring eld-level permissions on the PERMISSIONS administraon page 195
Time for acon – conguring eld-level permissions on the PERMISSIONS
administraon page 196
Field-level permission reports 199
Time for acon – 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
Creang a Drupal theme 207
Table of Contents
[
v
]
Time for acon – creang a Drupal theme by cloning the exisng theme 207
Overriding template les 211
Time for acon – creang a template le for the Recipe content type 212
Eding template les 214
Time for acon – eding a template le 214
Styling the page using CSS 221
Time for acon – styling the page using CSS 221
Summary 224
Chapter 10: A Database Perspecve of Fields 225
Key elements in Fields 226
Enes 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, Mulple Value Fields 253
Pop quiz – mulple values in elds 253
Pop quiz – understanding the Boolean eld 253
Pop quiz – seng default values 253
Pop quiz – adding list elds 254
Pop quiz – understanding mulple value widgets 254
Chapter 4, Taxonomy 254
Pop quiz – creang 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 conguring le eld sengs 255
Pop quiz – using the le eld 255
Pop quiz – conguring image eld sengs 255
Pop quiz – creang custom image styles 256
Chapter 6, Managing Field Display 256
Pop quiz – a rst look at the eld display sengs 256
Pop quiz – formang eld display in the teaser 256
Pop quiz – custom display sengs 256
Pop quiz – formang 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
applicaon in a short period of me without sacricing the applicaon funconality.
Drupal is not only an open source CMS, but is also a web applicaon development
framework. The extensible and exible architecture of Drupal can be used for any size of
business including freelancers, agencies, governments, universies, 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 Construcon 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 rewrien and redesigned in Drupal 7. It has been opmized to run faster and
work more eciently. It provides you with more eld type opons, and it has given you more
exible opons 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. Aer
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 conal 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 Perspecve of Fields explains the dierence 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
interacon 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
wrien in PHP and can run on many plaorms and environments including Windows, Mac
OS X, and Ubuntu Linux.
You may want to create an account with a website hosng 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 instrucons of how to complete a procedure or task, we use a tle secon

hide($content['comments']);
hide($content['links']);
print render($content);
?>
When we wish to draw your aenon to a parcular 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
menon the book tle in the subject of your message.

nd any errata, please report them by vising
ktpub. com/support
,
selecng your book, clicking on the let us know link, and entering the details of your errata.
Once your errata are veried, your submission will be accepted and the errata added to
any list of exisng errata. Any exisng errata can be viewed by selecng your tle from
/>.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media.
At Packt, we take the protecon 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
locaon 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 protecng 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 beer 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. Aer that we will get our hands
dirty with a hands-on tutorial on creang 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. Alternavely, 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 secons the website has, before starng
the project.


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status