Drupal Articles

Programmatically render a Drupal view with exposed filters, arguments, etc.

Embedding a view programmatically is not too difficult, but what about passing it a value for an exposed filter? This snippet gives a simple example of how to modify the View object in a more meaningful way before rendering it.

An unhappy combination: Drupal, Facebook, og:image, and https

Here's a bit of an esoteric issue that was a bit tricky to hunt down. I hope that this blog post helps the few people out there that it applies to!

You've got a secure (https) Drupal site, and you'd like it to contain facebook-compatible, open graph meta tags. So, you download the meta tags module and do some configuration magic. Everything looks good until you post a page on Facebook, and the image doesn't work!

Apparently, Facebook doesn't play nice with og:image tags served over https.

Ok, so let's not serve our og:image tags over https. A quick theming hook will fix that in no time:

Connecting Facebook with Drupal, the easy way: Part 2

In Part 1: Connecting Facebook with Drupal, the easy way I extolled the wonders of Facebook OAuth and showed off its excellent API with an example of how to map facebook user data to Drupal user objects.

For part 2, I'm going to show you another side of the API. But before we get started, I'd like to give a quick shout-out to Nathan Haug (@quicksketch), author of Facebook OAuth. I'm about to take a lot of text directly from the Nate's well-written README.txt, so don't be confused by the liberally-applied quotation marks.

Connecting Facebook with Drupal, the easy way.

There are a few heavyweight modules contending to provide Facebook integration for Drupal. Which module is right for you? Simply looking at the Reported installs and Maintenance Status won't present a clear answer.

Choices

A recent project of mine required the use of Facebook's OAuth service. Here are the three modules that I considered:

Decisions

I installed and configured each of these modules before coming to a decision. Here's a quick summary of what I found:

Building a Multistep Registration Form in Drupal 7

This article provides a step-by-step tutorial for creating a custom, multistep registration form via the Forms API in Drupal 7. For a Drupal 6 guide, I recommend Multistep registration form in Drupal 6.

Drupal 7's updated Form API makes the process of building multistep forms relatively painless. In combination with the excellent Examples for Developers module, it's really just a matter of copy, paste, and tweak.

We're going to be putting a slightly different spin on the standard approach to creating a multistep form.

Migrate Classes: Location CCK to Address Field

I've just begun to tackle migrating a site from Drupal 6 to Drupal 7 via V2 of the Migrate module. It's truly an excellent module, but like most things Drupal, it has a steep learning curve. To help offset that slope, I plan to post migration snippets on this blog throughout the next few weeks. The snippets should help to serve as starting points for the many different migration scenarios that you all may encounter. Here is a short list of tentatively planned examples:

Managing account registration for multiple user types

The Scenario

You're building a new Drupal site that needs to handle two distinct types of users: Consumers and Service Providers. Each user group must have a unique role, profile type, and registration page. Users of each type should be able to visit your site, find the correct registration page, fill out their profile, and be granted an account with the correct role. Sounds easy, right?

Well, I recently found myself in this exact scenario. I was surprised to find that no combination of modules would exactly fit these requirements. This seemed like a great opportunity to build a legitimate, contributed module. Here it is: Profile2 Registration Path.

The purpose of this blog post is to tell a bit of that story, but more importantly, to show you how to do use it!

The Journey

Jump to the Quick Step-by-step (TLDR)

Using your email address as your Drupal username

It can be difficult to remember all of the usernames and passwords that you use to log in to various websites across the internet, so why force users to create a new username for your web site? It's easier on everyone to simply combine the username and email address fields. It also cleans up your registration form a bit.

In Drupal, there are two modules that can help you to accomplish this:

These two modules are mutually exclusive— they are not compatible with each other, so you'll have to pick one. I prefer to use Email Registration, and I'll explain why.

Migrating a Drupal site between servers via Drush

As of Drush 4.5, migrating a Drupal site between servers became much easier. The new, little-known drush archive-dump and drush archive-restore commands make it an essentially three step process.

Overview

A basic Drupal site is made of two fundamental elements: the codebase and the database. When you migrate a Drupal site, you need to migrate both of these elements, often with a bit of re-configuration to boot.

The Old Way

Before using Drush to migrate a site, my standard procedure for site migration looked something like this:

Database Migration

  1. create a new, empty database
  2. create a new user
  3. associate the new user with the new database (with correct permissions)
  4. create a db dump from the source environment
  5. import the db dump to the target environment

Codebase Migration

Adding first and last classes to your field items

By default, Drupal's core field.module with add 'odd' and 'even' classes to your field items, but it won't add 'first' and 'last' classes! This can quickly be remedied by overriding the core field.tpl.php file with your own custom one.

I'd recommend first copying /modules/field/theme/field.tpl.php into your site's theme folder. For me, the destination was /sites/all/themes/grasmash/templates/field.tpl.php. Then clear your caches. This will force Drupal to check for new template files in your theme's directory, and select the new field.tpl.php as the prioritized template for generating fields.

Here's the business part of my customized field.tpl.php file:

Pages