Drupal - Change the Output of "Member for" on User Profiles

When viewing a user account profile page in Drupal, by default the "History" field is displayed like so:

Drupal Commerce Order Owner Autocomplete with E-mail Address

When creating an order as an admin in Drupal Commerce, we can specify the user that owns the order if we know their user name. This works well in most cases, but to be able to search by the user's e-mail address would be helpful too. I've had a few requests from clients for this feature. So without further ado, let's ado it!

 * Implements hook_menu().
function my_module_menu() {
  $items = array();
  $items['my_module/commerce/order-owner/autocomplete'] = array(
    'page callback' => 'my_module_commerce_order_owner_autocomplete',
    'access arguments' => array('configure order settings'),
    'type' => MENU_CALLBACK
  return $items;

 * Implements hook_form_FORM_ID_alter().
function my_module_form_commerce_order_ui_order_form_alter(&$form, &$form_state, $form_id) {
  // Attach our custom autocomplete to the order owner name input so we can
  // search by e-mail address.
  $form['user']['name']['#autocomplete_path'] = 'my_module/commerce/order-owner/autocomplete';

 * Queries results for the commerce order owner autocomplete.
function my_module_commerce_order_owner_autocomplete($string) {
  $matches = array();
  $query = db_select('users', 'u');
    ->fields('u', array('uid', 'name', 'mail'))
      ->condition("u.mail", '%' . db_like($string) . '%', 'LIKE')
      ->condition("", '%' . db_like($string) . '%', 'LIKE')
    ->condition('u.uid', 0, '<>')
    ->range(0, 10);
  $results = $query->execute();
  foreach ($results as $row) {
    $key = "$row->name";
    $matches[$key] = "$row->name - $row->mail ($row->uid)";

Drupal User Entity Reference Field with Custom Autocomplete that uses an Address Field

User reference fields (aka entity reference fields) are great. As you may have guessed, we can use these fields to reference users on other entities (e.g. nodes).

Say we had a user reference field on the Article content type... by default, when selecting a user to reference, we could configure the field widget to be an autocomplete. This allows us to begin typing the user's login name as a way to reference them. This works well in most cases.

What if we had an address field on our user entities, and collected the user's first and last names? It may be more usable for site administrators to be able to search across the user's actual name instead of their user name for logging in.

We can use hook_menu(), hook_form_BASE_FORM_ID_alter(), and a custom callback function to provide a custom autocomplete widget that searches across our address field's values instead...

EntityMetadataWrapperException: Unknown data property commerce_product. in EntityStructureWrapper->getPropertyInfo()

After struggling with this issue for days, and days, and days, and more days, I was finally able to debug it by placing the following line of code in the getPropertyInfo() function inside of the sites/all/modules/entity/includes/ file (be sure to have the Devel module enabled on your Drupal site):


The code above should go right before this line:

When should I use t() in Drupal?

This page contains information about when, and when not to use the t() function in Drupal 7 (and beyond). I learned this information from the Localization API, and will add more tid-bits as I learn them. Until then, t('thanks for stopping by!');

When to use it...


When to NOT use it...

In hook_menu() and/or hook_perm()

Text declared in hook_menu() and hook_perm() are automatically translated when displayed.

Drupal - Views Calendar Theme "Page by date" Navigation Title

I was using Views to display a calendar of events. I then attached a date navigation pager to the calendar. By default, the navigation title was coming out looking like this:

Build a Mobile App to Sell Products with Drupal

This tutorial describes how to create a website and mobile application to sell physical products. Customers on a desktop or laptop computer will be able to purchase the product through the website, much like a typical e-commerce store.

Once we have built the mobile application, customers who have downloaded and installed the mobile app onto their Android or iOS (iPhone, iPad) device will be able to purchase the product as an In-App Purchase.

For this example website and mobile app, we're going to sell bottles of beer. *Cough* - Please don't actually sell beer without first getting permission from your local Big Brother.

The main 3 sets of tools we will utilize are:

This tutorial was inspired by this Session from DrupalCon Austin 2014. If you're new to any of these tools mentioned above, please watch the video for an introduction. Otherwise, let's get started!

Drupal Batch Example

Drupal 7

Here's an example that utilizes the Batch API for Drupal 7:

Apple Mach-O Linker Error - missing required architecture arm64

When trying to "Product -> Archive" my PhoneGap project in xCode, I was getting this error:

missing required architecture arm64

To fix this, I went to the "Build Settings" for my project, and removed the arm64 from the Valid Architectures. That's it! Thanks to Joseph Ross for his continued iOS debugging skills.

Drupal - Missing bundle property on entity of type comment

After 24 hours of fudging around, I finally figured out what causes this problem. To make a long story short, I needed to make sure the node_type property was set on my comment entity. For example, a comment for an article node needs this property set:

$entity->node_type = 'comment_node_article';

The error message itself is a little misleading, since it says the bundle property is missing. But in actuality, it was looking for the node_type property to be set also.


Subscribe to RSS - Code