New Drupal 7 Book: Drupal 7 Module Development

January 7th 2011 was a happy day for all Drupal fans: Drupal 7 got officially released. The two Drupal books that I used to get over the steep learning curve of Drupal 6 module development, Drupal 6 Module Development by Packt Publishing and Pro Drupal Development by Apress, got released for Drupal 7.

While the later is the definite development reference for Drupal, the former was written in a more approchable way. The Drupal 6 version of the book included lots of real-life module examples that made becoming acquainted with Drupal's API function a fun way to learning module development for Drupal. I can't wait to work myself through the updated version, "Drupal 7 Module Development", and will post a detailed review of the book thereafter.

Book Cover

New Drupal Module: Views Search

A feature request for one of my recent projects was an "Advanced Search" functionality, similar to the ones that is commonly found in social networking and personal sites. Rather than creating a completely custom search form using Drupal's Forms API, I decided to create a module called "Views Search" which uses the Views module and allows anyone to create advanced searches for anything Views can process without custom coding.

Views' exposed filter work very similar to the search functionality, however exposed filters in the Views module work like true filters, where a list of nodes is limited down to the intended results using a combination of several filters. In contrast to this approach, a more traditional search form allows users to define all criteria first (before seeing any results) and then click on a "Search" button to see the results. This module allows site administrators to easily use the behavior of a traditional search for selected views.

In addition, the Views Search module allows Drupal site members to save their searches for future usage.

Here's an overview of the feature set:

  • Separates the search form and results: selected views show a search form without showing the results on the first page and the search results without showing the search form/filters on the following pages
  • Allows users to start over with a search (using a button on search results page)
  • Allows users to modify the search criteria (using a button on the search results page that expands search criteria using jQuery)
  • Allows users to save a set of search criteria for later usage
  • Allows users to modify/overwrite the name and search criteria for a saved search
  • Provides a default view that allows the current user to manage saved searches
  • Provides a block showing a drop-down with saved searches for the current user
  • Enables site administrators to select views that should act like "search views"
  • Enables site administrators to limit the number of saved searches for certain user roles & set saved search limits per rule
  • Enables site administrators to batch-delete all saved searches for a specific view

You can download the module from the Views Search Module page on Drupal.org. I'm looking forward to any feedback.

Comparison of Views Exposed Filter and Views Search

Drupal.org Redesign Imminent

Personally, I'm excited about the upcoming Drupal.org redesign. Not because I think that there is anything wrong with the old design or information architecture - I actually grew to love it. No: I'm mostly excited about the Drupal redesign because I think it will help to push Drupal out of the developer corner and make more people "like" Drupal because of a new design that looks less like a website for geeks only (again: I'm a proud one myself).

Currently, there are some small efforts to make the soon-to-be old Drupal.org look more friendly, but it seems clear to me that a major redesign was needed to communicate the following statement clearly and to the point: Drupal is the most powerful CMS out there.

In my earlier days as a developer, people unsuccessfully attempted to convince me of the power of "Ruby on Rails". It wasn't until I started working with Drupal that I found that promised power and ease-of-use, from a developer's perspective, of course. And that's the one thing that the current Drupal.org design is not communicating clearly: How powerful Drupal is, how many large organization love it, use it or want to start using it as soon as possible.

In the new design, I too miss the Druplicon looking at me from the top left corner. But I hear Larry in his blog post about the difference between the Druplicon and a logo, and it makes sense to me. One comment in the request to beta-test the new design concerned a lack of character in the new design. While I agree that the new design looks clean but a bit generic, I don't think the old design had a lot of character because of the visual look of the site itself. The character was coming from the personal involvement of the people using it, and that aspect will continue with the new design

I do think that developers using the Drupal.org issue system will need to adjust to the new look. However, while we will get used to the new look very quickly, Drupal newbies will be much more willing to dive into the beauty of Drupal because of the look of the new website.

Great job!

The new look of Drupal.org: Cleaner and simplified.
The old look of Drupal.org

SEO Module Alternative to Nodewords for Drupal Developers

After listening to Seth Cohn's very inspirational speech about the Context module at the DrupalCamp NYC 8, I got inspired to play around with that module a bit more. Primarily, Context serves as alternative way for the block administration. However, Seth focused more on Context's ability to keep variables during a page load available, without the need to use global variables or other workarounds.

One of my recent projects required very specialized SEO optimiziation. Initially, I started using the Nodewords module, which is brilliant and very easy to use. In certain areas however, I felt that Nodewords makes it harder for me as a developer to achieve my goals, so I wanted to come up with a different approach. By no means is this module supposed to be a replacement for Nodewords, it's more of an exploration of the Context module.

Search Engine Optimization for drupal module developers

In this example, I'm primarily focusing on the head title (the title that appears in the browser for any open window) and the meta description (which will show up as a teaser in search results).

I've identified three different "types" or areas that often require specialized SEO optimization. These are:

  1. Nodes (per content type)
  2. Pages (created by other custom Drupal modules)
  3. Views Pages (created by the Views module)

SEO Head: A centralized module that handles head title and meta description

If you're developing in a similar way as I am, you like to create smaller modules for each distinct functionality. In my approach for this alternative module, I am using a central module called SEO Head, which works as the central module to set the head title and meta description. This module is called at end of the page execution process by using a relatively high system weight of '200' (the default being 0). By calling this module at the end, I am trying to make sure that no other module sets the head title. This module overrides the existing head title and sets the meta description.

Looking at the above list, only one of the areas is defined within the module: Nodes, per content type. By the time the SEO Head module gets called, the full node object is available, so anything (including CCK fields) can be used for the page title or meta description. See lines 17 to 35 in the source code.

In this example implementation of the SEO Head module, I am using a special text CCK field for the page title of a node of the type 'page' (see the file attachments below, you can install this field easily by downloading the node_page_title module, which is a Features module.). For the regular "story" node type, the body of the story is used for the meta description. These two instances are just examples of what could be done here.

Using Context: Define head title and meta description in your custom module

Instead of defining all page titles and meta descriptions in one central place, I thought it would be much 'nicer' to have each custom module you create for your site define their page titles and meta descriptions, if needed. In order to make this happen, I am using the Context module as a vehicle to transport any head titles and meta descriptions to the SEO Head module. This allows any module to set these values 'remotely', meaning directly in the module. To demonstrate this, I created a separate example module called 'example', see download list below. The Example module provides a page and a simple view.

For any page defined in a module, just use the context_set function (line 13 and 14 in the example module) to set head title and/or meta description, and the SEO Head title will take care of the rest.

To define head title and meta description for Views (which is currently not possible with Nodewords), you can make use of the hook_views_pre_execute hook (line 23 following).

 

Again, these modules are just some examples for using Context in a creative way, and aren't meant to replace the Nodewords example in any way. I'm looking forward to some interesting discussions.

File: 

Drupal 7 Module Development - Liveblogging from Drupal Camp NYC 8

List of Changes

Converting 6.x modules to 7.x - Categorical

New Database API

  • Vendor-agnostic abstraction layer
  • Built on top of PHP's PDO library
  • Current drivers: MySQL, PostgreSQL, SQLite, SQL Server
  • Can still write SQL. Example: $result = db_query("SELECT nid, tilte FROM {node}");
  • Placeholders have changed. Strings don't have to be enclosed in single quotes any more, the Drupal database abstraction layer does that for you. Example:
    $sql = "SELECT title FROM {node} WHERE type = :type";
    $result = db_query($sql, array(':type' => 'page'));
  • Results sets from select queries are easier to loop over (e.g. with a foreach ($result as $record) { // do stuff }.
  • Dynamic Queries possible. Example too complicated to note down in 1 minute.

hook calls

  • hook_nodeapi: no more $op
  • Replace with several individual hooks
  • Same for hook_user and hook_blocks

Views for Developers - Live Blogging from Drupal Camp NYC 8

By: Frank Carey

Question: How to integrate your custom module with all the goodie-ness of views?

Shameless Plug: I attempted to write a similar post about this in this post: Views Integration with your custom module and Overwriting Views Handlers

The custom way to integrate your module is by using hook_views_data. Using this hook, you would define how your module exposes data to views. A great alternative is using the Table Wizard module.

Example: Create Custom Watchdog View using Table Wizard

(Following list is not an actual "step by step" list, but the general idea is that Table Wizard allows us to create the array required for hook_views_data in a much simpler way than coding it by hand).

  • Select "Add existing tables"
  • Select the table you want to expose
  • Select "Provide default View"
  • Create a new table wizard entry for users
  • Create a relationship bewteen the two using the relationship and join on uid (users.uid joins with watchdog.uid)
    Options here are manual or automatic (manual: relationship needs to be added manually in the view, automatic: happens in the background without option in views interface)
  • Select the new table wizard entry that was just created and then export the views definition for it

Context: Changing how to Code in Drupal - Liveblogging from Drupal Camp NYC 8

By Seth Cohn (sethcohn)

Get the Context module here: http://drupal.org/project/context
Another great module suggestion: http://drupal.org/project/menu_block (not necessarily related to Context though)

What's the idea of context

Context pays attention to what's happening on the page while the page is being rendered. Developers used to take arguments from the final URL path, but Context is saving a lot of information (such as type, nid, uid etc.) for later use (in the page load process). This is pretty awesome, because all content (such as the full node object) is already available in the Context. In short words: Smart global variables that persist throughout the whole (Drupal) page load.

The original description from the module page: Context allows you to manage contextual conditions and reactions for different portions of your site. You can think of each context as representing a "section" of your site. For each context, you can choose the conditions that trigger this context to be active and choose different aspects of Drupal that should react to this active context.

Basic construct of a context: Add a block using Context

  • Conditions: When should this context take effect?
    Example: This context should be active any time I look at a node with the type "page", but not on the edit form
  • Reactions: What should happen?
    Example: Show a block in a specifc region
  • Et Voila: Never use the blocks interface again

Another Example using Context Reaction: Theme

  • Condition: You're looking at a node type "page"
  • Add a specific body class to this page

Other Conditions

  • Node tagged with a specific item
  • Looking at a specific path (including wildcards)
  • Set this context for a specific user role
  • Show this context for specific views
  • All conditions can be combined with each other and can be used with either AND or OR

Other Module Integrations:

Building Features and Exportables (The Wave of the Drupal Future) - Live Blogging from Drupal Camp NYC8

By: Frank Carey (frankcarey)

Features Module Image

Why settings should be in the code and not the database? Common problems with Drupal

  • How to track changes?
  • How to revert back in the case of a mistake?
  • How can configurations be re-used?
  • How to work as a team?
  • How to update settings across sites?

How to accomplish this

  • Certain modules have their own import/export capabilities, such as Views, Migrate, Rules
  • Other modules use CTools export API, such as Panels, Context (current standard)
  • Use modules like Strongarm that export the variables table

Enter: Features (Features Module)

  • Features is basically an exportables manager
  • Creates a "features" module (which is just a normal module)
  • Provides GUi interface to choose what should be exported with features
  • Provides drush integration

What should a feature consist of?

  • Logical sections and "atomic" functionality
  • Examples: Blog, Photo Galleries

Features of Features

  • Shows all module dependencies, content types, image cache types and views
  • Recreate and modify an existing feature (template)
  • Export features (distribution)
  • Modify exported features distribution in the include files directly

Takeaway Notes from this presentation:

Hardcore Drupal developers know the following modules well. How could I have missed using Features so far?

Really advanced presentation. Frank: Congrats on the Sony gig :)

Drupal as a Web Services Platform for iPhone

By: Jingsheng Wang (skyredwang, http://insready.com/)

Some Stats:

  • 1.67 Billion Internet users. 4 Billion Mobile Phone Users. By 2013, mobile phone users with Internet access will surpass Internet users. Or earlier.
  • iPhone has the most page requests (55%), Android next (20%) then Blackberry (12%). Rest is minor.
  • Google experienced 5x more mobile search requests from 2008 to 2010
  • Simple App: 50K to 150K for a simple mobile app

How to expose Drupal APIs to external clients?

With the Drupal Services (http://drupal.org/project/services). Drupal Services adds a communication layer to your Drupal application. This module allows to output data in JSON, RPC etc.

Services Components:

  1. Servers (what should the output be?)
    1. XML-RPC
    2. JSON
    3. JSON-RPC
    4. REST
    5. AMFPHP
  2. Authentication
    1. API Keys
    2. Drupal Sessions (servers sessions can be re-used on mobile devices)
    3. OAuth
  3. Services
    1. Nodes
    2. User
    3. Taxonomy
    4. Files
    5. System
    6. Search
    7. Views Services
    8. Location
    9. Flag
    10. Any other infrastructure can be re-used (hook available for services module)

Example

Configuration: JSON_Server + Key Auth + Session
URL: http://site.com/services/json
POST: method:system.connect
Return: sessionID

Example URL: http://drupal.org/node/339845

Framework and Platforms

iPhone (IDE: X-Code, Language: Objective-C)
Android (IDE: Eclipse, Language, Java, C, C++)
PhoneGap (Open Source platform attempting to build cross-platform mobile apps, build apps in HTML)
Appcelerator Titanium API (Presentation from DrupalCon SF)

Libraries

(list taken from http://drupal.org/node/763066)

OAuth Handshake ("The future of authentication")

Used by sites like Facebook and Flickr. 3rd-party app makes a request to grant access to data. Username and password only need sto be provided to first-party site, not 3rd-party application.

Drush Introduction - Live Blogging from Drupal Camp NYC 8

By: Robbie (

Development Seed

Where to start:

Install

What can you do with drush?

  • Install Drupal with a command
  • Log directly into mysql (on the command line)
  • Download modules right into the correct location for Drupal (a few at once)
  • Enable modules from the command line
  • Create database dumps
  • Clear your cache (selectively)

So in a nutshell: Pure command line awesomeness. Great presentation!