You are here

public function GroupController::invoke in Group 7

Overridden to take care of adding the group creator.

If this functionality were placed in GroupController::save(), we'd find ourselves in a catch-22:

  • On the one hand we need a group id to save the creator's GroupMembership to the database, requiring us to first save new Group entities before saving their creator memberships.
  • On the other hand, saving a new entity causes the insert hook to be invoked, leaving room for buggy behavior should one try to load the newly created group's members in aforementioned hook.

To solve this without overriding EntityAPIController::save() and without having to fiddle with either module or hook weight, we place the code here because GroupController::invoke() is called right after the actual insert and we are sure to have a group id already.

Overrides EntityAPIController::invoke

See also

EntityAPIController::save()

File

classes/group.controller.inc, line 31
Defines the Entity API CRUD class for groups.

Class

GroupController
Controller for group entities.

Code

public function invoke($hook, $group) {
  if ($hook == 'insert') {

    // Retrieve the default roles for this group type.
    $group_type = group_type_load($group->type);
    $roles = isset($group_type->config['creator_roles']) ? $group_type->config['creator_roles'] : array();

    // Add the current user as the first member.
    global $user;
    $group
      ->addMember($user->uid, array(
      'roles' => $roles,
    ));

    // On the off chance that someone tried to load the group when acting upon
    // the GroupMembership save above, the field cache for this group needs to
    // be cleared. Otherwise, the newly created group will keep loading
    // without its field data available until the caches are cleared.
    //
    // For now, this is the approach we're taking seeing as the alternative is
    // to copy over EntityAPIController::invoke() entirely, forcing us to keep
    // track of the changes in that function until the world ends.
    cache_clear_all("field:group:{$group->gid}", 'cache_field');
  }

  // Continue running the actual invoke logic.
  parent::invoke($hook, $group);
}