học drupal CCK
Trang 2Drupal 7 Fields/CCK
Beginner's Guide
Explore Drupal 7 fields/CCK and master their use
Dave Poon
Trang 3Drupal 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
Trang 5About 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
Trang 6About 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,
Trang 7with 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
Trang 8Support 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 Packts online digital book library Here, you can access, read and search across Packt's entire library of books
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders
Trang 10Table of Contents
Chapter 1: Nodes and Content types in Drupal 7 7
Time for action – creating contents using the article content type 16
Time for action – creating the Cooking Tip content types 20
Time for action – creating the Recipe content type 25
Time for action – adding content using the Cooking Tip content type 27
Time for action – adding contents using the Recipe content type 30
Trang 11Time for action – finding the field types and input widgets in Field UI 40
Time for action – adding Long text fields using the text module 46
Time for action – adding text fields using the text module 51
Time for action – rearranging the order of the Notes field 53
Time for action – adding a text field to comments 55 Time for action – rearranging the order of the Email field 58
Time for action – reusing the Notes field for the Recipe content type 59 Time for action – reusing the Source field for the Recipe content type 60 Time for action – rearranging the order of the Notes field 61 Time for action – reusing the Email text field in the Recipe content type 62 Time for action – rearranging the order of the Email field 62
Trang 12Time for action – adding terms to Cooking Tip Category 98
Time for action – adding a term reference field to the Cooking Tip content type 103
Time for action – categorizing Cooking Tip Content using taxonomy 108
Time for action – adding a field to the Cooking Tip Category vocabulary 113
Adding and configuring file fields to content types 120 Time for action – adding and configuring a file field to the Recipe content type 120
Time for action – adding an image field to the Recipe content type 134
Time for action – configuring an image field for the Recipe content type 136
Time for action – creating a custom image style 141
Time for action – a first look at the field display settings 148
Time for action – formatting the Body field display in teaser view 153
Time for action – enabling the Full content view mode in custom display settings 158 Formatting the image field display on Full content 160 Time for action – formatting the image field display on Full content 160
Trang 13Time for action – formatting the comment field display 163
Time for action – formatting the vocabulary field display 165
Time for action – installing a module using Update manager 180
Time for action – downloading the Field Permission module 188 Installing and enabling the Field Permission module 189 Time for action – installing and enabling the Field Permission module 190
Time for action – enabling field-level permissions 192 Configuring field-level permissions on the PERMISSIONS administration page 195 Time for action – configuring field-level permissions on the PERMISSIONS
Time for action – the Field permissions report page 199
Trang 14Time for action – creating a Drupal theme by cloning the existing theme 207
Time for action – creating a template file for the Recipe content type 212
Chapter 10: A Database Perspective of Fields 225
Chapter 1, Nodes and Content types in Drupal 7 251
Pop quiz – understanding the default content types 251
Pop quiz – adding content using custom content types 251
Trang 15Chapter 2, Fields 252
Pop quiz – finding the field types and input widgets in Field UI 252Pop quiz – adding text fields using the text module 252
Pop quiz – rearranging the order of fields in comments 253
Pop quiz – adding term references to content types 254
Pop quiz – adding and configuring file field settings 255
Pop quiz – a first look at the field display settings 256Pop quiz – formatting field display in the teaser 256
Pop quiz – formatting image field display on Full content 256
Trang 16Chapter 8, Field Level Permissions 257
Pop quiz – installing and enabling the Field Permission module 257
Pop quiz – configuring field-level permissions on the PERMISSIONS
Pop quiz – creating a Drupal theme by cloning the existing theme 258Pop quiz – creating a template file for the Recipe content type 258
Trang 18Drupal 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
Trang 19What 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
Trang 20Chapter 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
Trang 21Instructions 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
// 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
Trang 22New 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
Trang 23Although 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
Trang 24Nodes and Content types in Drupal 7
At the beginning of this chapter, we will have a look at a preview of our final
project, it will give you a better idea of what we need to do, and what will be
achieved when we reach the end of this book We will also explain the concept
of nodes and content types in terms of Drupal After that we will get our hands
dirty with a hands-on tutorial on creating content and content types using
the default content types in Drupal We will go even further to create our own
custom content types and add more content by using these custom content
types to create a website Sounds cool! Let's get started!
We shall discuss the following topics in this chapter:
The book example project
A preview of the final project
What is a content type?
Default content types in Drupal 7 core
Creating content using article content types
Creating custom content types
Adding content using custom content types
Trang 25The book example project
This book will show you how to create a website using Drupal 7 and Fields Instead
of explaining how the Fields module works in theory, we will learn about it by creating a fictional project
We will guide you through the complete project by following a set of steps or tasks, and thus you will learn:
How to create custom web content by creating your own content types and fields
How to create fields for displaying multiple values
How to create taxonomy vocabularies and terms for categorizing web content
How to use the file field to provide file uploading functionality to the website
How to use the image field to provide image uploading, extension validation, and image processing functionality to the website
How to format images using image styles
How to control the display of fields in teasers and full content view modes
How to set field level permissions for specific fields that only authenticated users can access
Finally, how to theme fields using templates and CSS
You also have a choice of downloading the final project file from www.packtpub.com
or www.drupal7fieldsbook.com to follow along with the book Alternatively, you can compare your project with the final project if you have any problem while following the book
A preview of the final project
The website that we are going to build in this book is a cooking website filled with recipes Let's take a look at some of the most important sections the website has, before starting the project
Trang 27This 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
Trang 28The 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
Trang 29The 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
Trang 30We 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
Trang 31It 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
Trang 32The 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,
Trang 33The 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
Trang 342. 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 folder 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
Trang 354. After filling in all the details, we leave the other settings to their defaults Then go to
the bottom 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 content, and it will show a message saying the content has been successfully created:
Trang 366. If you go back to the homepage, you will see the content listed on the page as in the following 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 Page and Image content type
b Basic page and Page content type
c Article and Basic page content type
d Article and Page content type
2 To which default content type can we attach images?
a Image content type
b Article content type
c Basic page content type
Trang 37Creating 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 between the Content and Appearance, just click on it.
Trang 382. A page will show up with the following options We can ignore the other options
for 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 existing content types in your Drupal site Currently it only shows the default content types:
Trang 394. 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:
Trang 406. 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