Home Web-development Drupal 7 Cookbook

Drupal 7 Cookbook

By Dylan Spencer James
books-svg-icon Book
Subscription
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
Subscription
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Installing and Configuring Drupal
About this book

Drupal 7 is a modern Content Management System famed for its flexibility and power. Using Drupal you can easily create custom functionality that would otherwise have to be purchased in many of the other leading CMSs.

"Drupal 7 Cookbook" is filled with recipes to help you to do more with Drupal and improve your skills. Chapters range from content creation, to theming, to managing your site. You will learn how to create your own content types and use them to create Views, Blocks, and Pages. This book will take you from novice to pro in just 12 chapters.

In a wide variety of practical recipes, you will learn how to work with views and panels, how to provide translations for your content to create a multilingual site, and to integrate your site with social media. You can develop the Zen starter theme or learn how to create custom cross-browser compatible Drupal themes, including themes for mobile devices. The Drupal 7 Cookbook contains all of the means necessary to take your skills from those of a novice Drupal user to a proficient site builder.

Publication date:
July 2012
Publisher
Packt
Pages
324
ISBN
9781849517966

 

Chapter 1. Installing and Configuring Drupal

In this chapter, we will cover:

  • Installing Drupal

  • Installing Drupal distributions

  • Installing modules and themes

  • Setting up the site search

  • Creating a multi-site Drupal installation

Introduction

Throughout this chapter, we will explore the process of setting up a Drupal website. We will see how to install the official Drupal 7 release and also how to install third-party Drupal distributions which are preconfigured in a variety of ways.

Following the installation, we will move on to the installation of third-party modules and then how your site can be configured to provide a site search form.

The chapter culminates with a recipe on setting up Drupal multi-site where two Drupal sites are configured to run from the same core files.

 

Introduction


Throughout this chapter, we will explore the process of setting up a Drupal website. We will see how to install the official Drupal 7 release and also how to install third-party Drupal distributions which are preconfigured in a variety of ways.

Following the installation, we will move on to the installation of third-party modules and then how your site can be configured to provide a site search form.

The chapter culminates with a recipe on setting up Drupal multi-site where two Drupal sites are configured to run from the same core files.

 

Installing Drupal


There are a number of different ways to install Drupal on a web server, but in this recipe we will focus on the standard, most common installation, which is to say, Drupal running on an Apache server, which runs PHP with a MySQL database. In order to do this we will download the latest Drupal release, and walk you through all of the steps required to get it up and running.

Getting ready

Before beginning, you need to ensure that you meet the following minimal requirements:

  • Web hosting with FTP access (or file access through a control panel).

  • A server running PHP 5.2.5+ (5.3+ recommended).

  • A blank MySQL database and the login credentials to access it.

  • Ensure that register globals is set to off in the PHP.ini file. You may need to contact your hosting provider to do this.

How to do it...

  1. 1. The first step is to download the latest Drupal 7 release from the Drupal download page, which is located at http://drupal.org/project/drupal:

    This page displays the most recent and recommended releases for both Drupal 6 and 7. It also displays the most recent development versions, but be sure to download the recommended release (development versions are for developers who want to stay on the cutting edge).

  2. 2. When the file is downloaded, extract it and upload the files to your chosen web server document root directory on the server. This may take some time.

  3. 3. Configure your web server document root and server name (usually through a vhost directive).

  4. 4. When the upload is complete, open your browser and in the address bar, type in the server name configured in the previous step to begin the installation wizard.

  5. 5. Select Standard option and then select Save and continue:

  6. 6. The next screen that you will see is the language selection screen; there should only be one language available at this point. Ensure that English is selected before proceeding:

  7. 7. Following a requirements check, you will arrive at the database settings page. Enter your database name, username, and password in the required fields. Unless your database details have been supplied with a specific host name and port, you should leave the advanced options as they are and continue.

  8. 8. You will now see the Site configuration page. Under Site information enter the name you would like to appear as the site's name.

  9. 9. For Site e-mail address enter an e-mail address.

  10. 10. Under the SITE MAINTENANCE ACCOUNT box, enter a username for the admin user (also known as user 1), followed by an e-mail address and password:

  11. 11. In the Server settings box, select your country from the drop-down, followed by your local time zone.

  12. 12. Finally, in the Update notification box, ensure that both options are selected. Click on Save and continue to complete the installation. You will be presented with the congratulations page with a link to your new site.

How it works...

On the server requirements page, Drupal will carry out a number of tests. It is a requirement that PHP "register globals" is set to off or disabled.

Register globals is a feature of PHP which allows global variables to be set from the contents of the Environment, GET, POST, Cookie, and Server variables. It can be a major security risk, as it enables potential hackers to overwrite important variables and gain unauthorized access.

The Configure site page is where you specify the site name and e-mail addresses for the site and the admin user. The admin e-mail address will be used to contact the administrator with notifications from the site, and the site e-mail address is used as the originating e-mail address when the site sends e-mails to users. You can change these settings later on in the Site information page in the Configuration section.

It's important to select the options to receive the site notifications so that you are aware when software updates are available for your site core and contrib modules; important security updates are available from time to time.

There's more...

In this recipe we have seen a regular Drupal installation procedure. There are various different ways to install and configure Drupal. We will explore some of these alternatives in the following sections. We will also cover some of the potential pitfalls you may come across with the requirements page.

Uploading through a control panel

If your web-hosting provider provides web access to your files through a control panel such as CPanel, you can save time by uploading the compressed Drupal installation package and running the unzip function on the file, if that functionality is provided. This will dramatically reduce the amount of time taken to perform the installation.

Auto-installers

There are other ways in which Drupal can be installed. Your hosting may come with an auto-installer such as Fantastico De Luxe or Softaculous. Both of these services provide a simple way to achieve the same results without the need to use FTP or to configure a database.

Database table prefixes

At the database setup screen there is an option to use a table prefix. Any prefix entered into the field would be added to the start of all table names in the database. This means that you could run multiple installations of Drupal, or possibly other CMSs from the same database by setting a different prefix. This method, however, will have implications for performance and maintenance.

Installing on a Windows environment

This recipe deals with installing Drupal on a Linux server. However, Drupal runs perfectly well on an IIS (Windows) server. Using Microsoft's WebMatrix software, it's easy to set up a Drupal site.

http://www.microsoft.com/web/drupal

Alternative languages

Drupal supports many different languages. You can view and download the language packs at http://localize.drupal.org/download.

You then need to upload the file to Drupal root/profiles/standard/translations. You will then see the option for that new language in the language selection page of the installation.

Verifying the requirements page

If all goes to plan, and the server is already configured correctly, then step 3, the server requirements page, will be skipped. However, you may come across problems in a few areas:

  • Register Globals: This should be set to off in the php.ini file. This is very important in securing your site. If you find that register globals is turned on, then you will need to consult your hosting provider's documentation on this feature in order to switch it off.

  • Drupal will attempt to create the following folder: Drupal root/sites/default/files. If it fails, you may have to manually create this file on the server and give it the permission 755.

  • Drupal will attempt to create a settings.php file by copying the default.settings.php file.

  • If Drupal has trouble doing this, copy the default.settings.php file in the following directory: Drupal root/sites/default/default.settings.php and rename the copied file as settings.php.

  • Give settings.php full write access CHMODD 777. After Drupal finishes the installation process, it will try to set the permission of this file to 444; you must check that this has been done, and manually set the file to 444, if it has not.

See also

See Installing Drupal distributions for more installation options using a preconfigured Drupal distribution.

For more information about installing Drupal, see the installation guide at Drupal.org:

http://drupal.org/documentation/install

 

Installing Drupal distributions


Drupal distributions are third-party Drupal packages where the official Drupal release has been re-packaged to contain extra features. Drupal distributions can configure a complete Drupal site ready for a specific task, eliminating the need to research and install third-party modules.

Getting ready

The requirements for installing Drupal distributions are the same as for installing the Drupal core, as described in the preceding recipe Installing Drupal. You will need the following:

  • Web-hosting with FTP access (or file access through a control panel)

  • A server running PHP 5.2.5+ (5.3+ recommended)

  • An empty MySQL database and the login credentials to access it

How to do it...

  1. 1. For this example we will use the Commerce Kickstart distribution. Download this installation package from here: http://drupal.org/project/ commerce_kickstart

  2. 2. Configure your web server's server name and document root.

  3. 3. After the download is complete, extract the files, and upload the files to your web space document root using your FTP client of choice.

  4. 4. Once the upload is complete, navigate to the root (using the server name configured in your web server) of your website, using your browser to start the installation wizard.

  5. 5. Complete all the steps up to the Site configuration (see the previous recipe, Installing Drupal).

  6. 6. After completing the site configuration step you will arrive at a screen entitled Example store that provides installation options specific to the Drupal Commerce Kickstart distribution. Select both of the boxes on this screen to generate sample content for the Drupal commerce site.

How it works...

Now that you've learned how to install Drupal distributions, the distribution world is your oyster! There is an increasing range of different Drupal distributions to choose from that cater for a wide variety of websites, from conference organization to e-learning.

See also

 

Installing modules and themes


It is entirely possible to run a very successful Drupal website out of the box without adding any third-party modules. However, this approach is limited, and your needs may grow out of the built-in core functionalities. With just a few additional modules, the functionality of your site can be significantly expanded.

In this recipe, we will first see how to install the Link module from the Drupal.org website, followed by how to activate and configure it. The Link module adds a Link field to the list of available fields that can be used to create new content types.

How to do it...

  1. 1. Go to http://drupal.org/project/link.

  2. 2. Examine the available releases of Link, and find the most recent Drupal 7 release; copy the URL for the tar.gz file.

  3. 3. In Drupal, select Modules from the admin menu, followed by Install new module.

  4. 4. Paste the URL into the Install from a URL field and click on Install.

  5. 5. After the module is installed, select Enable newly added modules.

  6. 6. Look through the list of installed modules for the FIELDS fieldset. Select the checkbox for Link.

  7. 7. Click on Save configuration to finish:

How it works...

We begin by going to the project page for the module we are installing. In this case, the Link module. We then look for the most recent recommended release of the module.

Note

On the module's page it is likely that there will be two releases for both the Drupal 6 and Drupal 7 versions of the module. The files with the green background are the recommended releases. Be sure to use this version wherever possible. Sometimes there will only be an alpha or development release; try to get the highest version number.

After copying the link to the module's file, we go to the module installation page, and paste the URL to the module's file, and click on Install. Drupal then copies the file to the server, and extracts it into its own directory in the modules folder of the site.

After the module has been copied, we go back to the modules list, and enable the newly installed module by checking its checkbox and clicking on Save configuration to finish.

There's more...

There are more ways to install modules in Drupal. It's up to you to decide which method you prefer, but some other methods are described in the following sections.

Manually installing a module

The preceding method described uses the install from URL method. You may want to install a module manually. In which case simply download and extract the module package and move the extracted files to the Drupal root/sites/all/modules directory.

Installing a module with the Drush command-line tool

The process of installing modules can be made even easier by using the Drush tool to issue installation commands.

See also

  • Running commands with the Drush tool recipe in Chapter 12, Running Drupal

 

Setting up site search


In this recipe, we will see how to configure Drupal's native site search. The search feature is a part of the Drupal core module set. We will start by enabling the module, and then proceed to set up the search index. Finally, we will move the search block to the header.

How to do it...

  1. 1. Log in to your site and select Modules from the admin menu.

  2. 2. Locate the Search module, which is under the Core category, and make sure the checkbox is selected.

  3. 3. To enable the module, scroll to the bottom of the page and click on Save configuration.

  4. 4. Select Configuration from the admin menu.

  5. 5. Select Search settings from the SEARCH AND METADATA category.

  6. 6. Under INDEXING THROTTLE leave the Number of items to index per cron run at 100.

  7. 7. Under the INDEXING SETTINGS leave the Minimum word length to index as 3 and leave Simple CJK handling checked.

  8. 8. Under Active search modules ensure that Node and User are checked.

  9. 9. Under Default search module ensure that node is selected.

  10. 10. Under CONTENT RANKING set Keyword relevance to 1.

  11. 11. Click on Save configuration and close the admin panel when the save is complete.

  12. 12. To place the Search form in the header, select Structure from the admin menu.

  13. 13. Select Blocks from the Structure menu to go to the blocks management page.

  14. 14. Search through the blocks list to find the Search form block and set its position to header by updating the drop-down menu.

  15. 15. Scroll down the page and click on Save blocks.

How it works...

The site search works by periodically indexing the pages of a site. The indexing is activated by a cron job at a predetermined interval.

Cron is the process that periodically activates tasks such as search indexing, checking for updates, and clearing the cache. Drupal 7 has its own method to periodically call the cron script; however, it's more preferable to call Drupal's cron script directly from the server by setting up a crontab for optimum performance.

It is important that you ensure that the cron is configured to run at a suitable interval. If you have lots of frequently changing content, then you will want to configure the cron to run more frequently, or less frequently if your content changes rarely. It's a trade-off between server load and search results freshness.

The Indexing status category on the search configuration page displays the percentage of content indexed. This indicates how much of the eligible content is indexed for the search function. Don't worry if your content isn't immediately indexed. The indexer is configured to only index a finite number of pages per cron run. The number of pages indexed per run is set in the Indexing throttle category. You must be careful that the throttle is not set so high that it can overload your server.

You can manually activate the cron job, and therefore, activate the search indexing procedure, by going to Configuration | Cron | Run cron.

The Content ranking option will determine the prominence of each of the listed factors; a weight of 0 being no prominence and a weight of 10 being highest prominence. The actual settings that you choose for this should depend on the type of search results you or your organization favor.

The search form block will be set to display in the first column by default. Moving it to the header is just a personal preference, with the added purpose of demonstrating how easy it is to move the block.

 

Creating a multi-site Drupal installation


Drupal's multi-site feature allows multiple sites to run from the same core code, while having separate databases, configurations, and user files. Multi-site functionality removes the pain in managing multiple cores and multiple sets of modules, meaning that there is only one primary installation that needs to be updated and managed. It also reduces the amount of disk space that is required.

In this recipe, we will see how to prepare the secondary site's domain so that it points to the primary site's Drupal installation. We will then see how to activate and install the multi-site functionality on the primary site.

Getting ready

You will need the following:

  • An existing Drupal installation for your primary site

  • A domain name for your secondary site

  • The ability to update your secondary domain to point to the name server of your primary domain

  • You will need the ability to create an add-on domain for your primary site (we will see how to actually create the add-on domain in the recipe)

  • A blank database

How to do it...

  1. 1. Update your secondary domain's name server to be the same as the primary domain's name server.

  2. 2. On your primary server, log in to your server's admin panel and create a new add-on domain. Add the URL of the secondary domain as the new domain name.

  3. 3. Add a username and password. This is to enable you to provide specific FTP access to the secondary domain without providing FTP access for your primary domain. Set the document root to be the location on the server of your primary Drupal installation:

  4. 4. On your primary site create a new folder inside your sites directory which has the same name as your secondary site, for example, /Drupal root/sites/secondary-site.com/.

  5. 5. Copy default.settings.php from the following folder: /Drupal root/default/ to the /Drupal root/sites/secondary-site.com/ folder.

  6. 6. Rename the newly copied file to settings.php and set its permission to 666:

  7. 7. Under /Drupal root/sites/secondary-site.com/, create a new folder called files and set the folder permission to 777.

  8. 8. Go to your secondary site in your browser; you will see the standard installation screen.

  9. 9. Complete the installation wizard as described in the first recipe of this chapter (Installing Drupal). On the database settings page enter the login credentials of your newly created empty database.

  10. 10. After completing the installation, set the permissions of Drupal root/sites/secondary-site.com/settings.php to 444.

How it works...

Updating the name server is usually done through the domain registrar. The name server you need to enter for the secondary domain will most likely be provided by your hosting provider for your primary domain in the welcome e-mail. You may need to wait some time for the DNS records to propagate before your domain name resolves to the newly appointed server.

Creating an add-on domain allows you to set up a third-party domain to point to your hosting directory without having to redirect the user. This means that the URL in the address bar of the user's browser will remain constant, even though they are accessing the directory of another site. The setup procedure for configuring an add-on domain may be different with different hosting providers and control panels, and this example references the cPanel management panel. If you find difficulty getting this to work, reference your hosting provider's documentation for add-on domains.

About the Author
  • Dylan Spencer James

    Dylan James is an open source developer from and based in South Wales. After gaining a Computer Science degree from Cardiff University, Dylan got experience as a database developer for Walkers snack foods before going on to work at the Welsh media company, Tinopolis Interactive, where he worked as a Lead Flash developer on some large projects for clients such as the BBC, LearnDirect, the MoD, and The Cambridge University Press. It was at Tinopolis that Dylan got his first taste of developing using open source platforms, delivering sites for clients using Joomla and Moodle. After leaving Tinopolis in 2010, Dylan began working at a web agency in Swansea, creating Drupal websites for some high profile clients. For updates and errata, please visit www.dylanspencerjames.com

    Browse publications by this author
Drupal 7 Cookbook
Unlock this book and the full library FREE for 7 days
Start now