Organic Groups
This page has been written to be up-to-date with Organic Groups 7.x-2.2
Organic groups 7.x-2.x is built to work with the Migrate module (https://drupal.org/project/migrate). No additional modules is needed.
Groups
Create a migration class for your group entity or content type as you normally would using MigrateDestinationNode or your own MigrateDestinationEntity/MigrateDestinationEntityAPI subclass. On this entity you will have two fields which are related to Organic Groups, normally named group_group and og_roles_permissions.
group_grouptakes a value 0 or 1. 1 means that the node or entity is a group.og_roles_permissionsalso takes a value 0 or 1. In this case, 1 means that the group may define its own roles and permissions.
In case you are happy with the default values for these fields applying to all your imported groups, you don't have to do anything. Your migration will just work. However, it is possible to change the default values:
// Organic Groups
$this->addFieldMapping('group_group')->defaultValue(1);
$this->addFieldMapping('og_roles_permissions')->defaultValue(0);
Group content
Once you've created a migration class for your content it is really easy to make your content group content. Your groups audience field (usually called og_group_ref) can be used as any other entity reference field:
// OG Audience field
$this->addFieldMapping('og_group_ref', 'remote_group_id')
->sourceMigration('YourGroupMigration');
Group roles
As far as I know there is no way yet to migrate OG roles. If you know a way please feel free to share your knowledge.
Group membership (simple)
When migrating your users there are two ways to set group membership. We'll first discuss the simplest way which can be implemented in the same class where you've set MigrateDestinationUser as your destination.
The plus side of this method is that you can import your users and set their group membership at the same time in one migration. The downside is, that you will not be able to set other properties on the membership itself. In case you need to import data like membership state, OG roles or other fields on the membership entity, take a look at the advanced method below.
Adding a user to a group works similar to adding content to a group. On the user entity you will find a group membership field which is usually called og_user_node. This field can be populated as any other Entity Reference field:
// OG Membership
$this->addFieldMapping('og_user_node', 'remote_group_id')
->sourceMigration('YourGroupMigration');
Make sure to depend your migration on the Group migration by specifying a dependency in hook_migrate_api():
function hook_migrate_api() {
$migrations = array(
...
'migrations' => array(
'YourUserMigration' => array(
'class_name' => 'YourUserMigrationClass',
'dependencies' => array(
'YourGroupMigration',
),
),
),
...
);
return $migrations;
}
To grant users OG roles during user migration, the following snippet can be added to the user migration:
/**
* Called immediately after the complete destination object is saved.
*
* @param $new_user
* The user object saved by the migration.
* @param $row
* Raw source data object - passed through to prepare handlers.
*/
public function complete($new_user, stdClass $row) {
// Grant roles to all groups the user is a member of.
$groups = field_get_items('user', $new_user, 'og_group_node');
foreach ($groups as $group) {
$gid = $group['target_id'];
$og_roles = og_roles('node', NULL, $gid);
foreach ($row->all_roles as $role_name) {
// Check for the existence of the OG role
if ($rid = array_search($role_name, $og_roles)) {
og_role_grant('node', $gid, $new_user->uid, $rid);
}
}
}
}
Group membership (advanced)
Another way of importing memberships is by using a separate migration. This method is generally more flexible than the method above and it lets you import data for custom fields on the membership entity.
Note that as of now, there is no way of providing the membership entity type. The used type will default to the first best matching group-audience field the user has access to.
class YourOGMembershipMigration extends Migration {
public function __construct() {
parent::__construct();
$this->description = t('Migrate OG memberships.');
// Create a map object for tracking the relationships between source rows
$this->map = new MigrateSQLMap($this->machineName,
array(
'gid' => array(
'type' => 'int',
'not null' => TRUE,
'description' => 'ID of the group.',
),
'uid' => array(
'type' => 'int',
'not null' => TRUE,
'description' => 'ID of the user.',
),
),
MigrateDestinationOGMembership::getKeySchema()
);
// Query the datasource.
$query = ...
$this->source = new MigrateSourceSQL($query);
$this->destination = new MigrateDestinationOGMembership();
// Group reference
$this->addFieldMapping('group_type')->defaultValue('node');
$this->addFieldMapping('gid', 'gid')
->sourceMigration('YourGroupMigration');
// User reference
$this->addFieldMapping('entity_type')->defaultValue('user');
$this->addFieldMapping('etid', 'uid')
->sourceMigration('YourUserMigration');
// Other membership properties
$this->addFieldMapping('state')->defaultValue(OG_STATE_ACTIVE);
$this->addFieldMapping('is_admin')->defaultValue(0);
// Array of roles names or ids.
$this->addFieldMapping('group_roles', 'roles');
}
}
Fields
group_type - The group entity type.
gid - The group entity id. Make sure to let the mapper translate your external values using sourceMigration().
entity_type - The entity type of the member. This will usually be 'user'.
etid - The entity ID of the member, usually the Drupal user ID. Make sure to let the mapper translate your external values using sourceMigration().
state - User's group membership can be OG_STATE_ACTIVE or OG_STATE_PENDING. Defaults to OG_STATE_ACTIVE.
created - Create date for this membership. Defaults to REQUEST_TIME.
group_roles - Array of role names. These roles will be assigned to the user (or other referenced entity) for the referenced group. OG_ADMINISTRATOR_ROLE corresponds to the group administrator role.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion