You are here

Extending Drupal Commons with Feature Alter Hooks

Drupal Commons provides a great framework for building community-driven sites. With topic (organic) groups at its core, Commons uses several content types to help grow communities such as

  • wikis to promote collaboration among users
  • polls for users to vote on
  • events for users to organize meet-ups and functions

These content types are organized into Features. However as we shape Commons to match our business requirements, we inevitability need to modify these Features accordingly. For example, our business requirements dictate that events

  1. Are only accessible by a custom "Event Manager" role
  2. Require an additional field for the event's host

One solution is to disable the Commons' Event feature and create our own custom Event feature in its place. While this allows you to fully customize the Feature, you are essentially building the feature from scratch. At least half the work you will be doing is recreating functionality that already exists. Enter Feature Component Hooks. These hooks provide you a quick and easy method for modifying Features. They follow the same paradigm as other alter methods: use them if you need to modify what a module has already provided. In the case of our event images problem, we can examine the following code.

function mymodule_user_default_permissions_alter(&$permissions) {
  $permissions['create event content'] = array(
    'name' => 'create event content',
    'roles' => array(
      '0' => 'Event Manager',
    ),
  );
}

This alters the existing permissions set by Commons to use our event manager role. To add the host field, it's business as usual using the simple point-and-click interface of the Features UI. Export the feature and add the previously mentioned code snippet. We've just extended Commons without hacking or rebuilding a contributed module.

SHARE SOCIAL
Developer
Specializing in back-end development, Raphael Lagman has been with Appnovation since March of 2012. He has over six years of web development experience with an expertise in open-source technologies. Raphael's favourite part of web development is its fast pace and consistently evolving nature....→ More about Raphael
Developer

Specializing in back-end development, Raphael Lagman has been with Appnovation since March of 2012. He has over six years of web development experience with an expertise in open-source technologies. Raphael's favourite part of web development is its fast pace and consistently evolving nature.

Outside of work, Raphael enjoys staying active at the gym and tinkering with his home computer.

Appnovation Technologies