[Drupal] How to add members with administrator role in Drupal Commerce store?

| | 3 min read

This article focuses on how to add new members to Drupal commerce store. Usually, when a new user created with admin role tries to access the store, the action will be restricted with an "Access denied" message. For a user to access the commerce store, he should be a member of the store. Let's find out how to resolve this issue.

On creating a store 'member_id' and 'user_id' will be created in 'commerce_store_role' table. In hook_entity_insert(), collect all the administrative user id's from user's table along with member_id and admin id. Insert the user id and member id or admin id in table 'commerce_store_user_role' so that user can access commerce store.

Inside the hook_entity_insert() implementation, we need to fetch users in commerce store who has the administrator role, and also fetch the members in store with role administrator. The constant 'COMMERCE_STORE_ADMINISTRATOR_ROLE' stands for a role name of store administrator.

/**
 * Implements hook_entity_insert().
 */
function custom_module_entity_insert($entity, $type) {
  if ($type == 'commerce_store') {
    $query = db_select('users', 'users');
    $query->innerJoin('users_roles', 'users_roles', 'users_roles.uid = users.uid');
    $query->innerJoin('role', 'role', 'role.rid = users_roles.rid');
    $query->condition('role.name', 'administrator');
    $query->fields('users', array('uid'));
    $rows = $query->execute();
    $admin_users_list = $rows->fetchAll();
    $query = db_select('commerce_store_role', 'com_str_rol');
    $query->condition('com_str_rol.name', array(COMMERCE_STORE_AUTHENTICATED_ROLE));
    $query->condition('com_str_rol.store_id', $entity->id);
    $query->fields('com_str_rol', array('rid'));
    $member_rid_query = $query->execute();
    $member_rid_lists = $member_rid_query->fetchAll();

    foreach ($member_rid_lists as $member_rid_list) {
      $member_rid = $member_rid_list->rid;
    }

    $query = db_select('commerce_store_role', 'com_str_rol');
    $query->condition('com_str_rol.name', array(COMMERCE_STORE_ADMINISTRATOR_ROLE));
    $query->condition('com_str_rol.store_id', $entity->id);
    $query->fields('com_str_rol', array('rid'));
    $admin_rid_query = $query->execute();
    $admin_rid_lists = $admin_rid_query->fetchAll();
    foreach ($admin_rid_lists as $admin_rid_list) {
      $admin_rid = $admin_rid_list->rid;
    }

    foreach ($admin_users_list as $admin_user) {
      $admin_users[] = $admin_user->uid;
      if ((isset($member_rid)) || (isset($admin_rid))) {
        db_insert('commerce_store_user_role')->fields(array('uid' => $admin_user->uid, 'rid' => $member_rid))->execute();
        db_insert('commerce_store_user_role')->fields(array('uid' => $admin_user->uid, 'rid' => $admin_rid))->execute();
        commerce_store_access_rebuild_permissions(array($entity->id));
      }
    }
  }
}

The constant 'COMMERCE_STORE_AUTHENTICATED_ROLE' is the role name of store member. We will get the users with admin roles from the '$admin_users_list' variable and member id from '$member_rid' in the above code snippet. Finally, we will insert the user id 'uid' to 'commerce_store_user_role' table. After it is done, call 'commerce_store_access_rebuild_permissions()' function to rebuild store access permissions.

Following the above steps permission will be add to the newly created store user. We may find out the permission added to user not displayed in the listing page. For updating store members list page use hook_entity_presave(). Here we will be once again fetching all users with role 'administrator' and assigns the admin id as commerce store target id.

/**
 * Implements hook_entity_presave().
 */
function custom_module_entity_presave($entity, $type) {
  if ($type == 'commerce_store') {
    $members = array();
    if (isset($entity->cmp_m_store[LANGUAGE_NONE])) {
      foreach ($entity->cmp_m_store[LANGUAGE_NONE] as $item) {
        if (isset($item['target_id'])) {
          $members[] = $item['target_id'];
        }
      }
    }

    $query = db_select('users', 'users');
    $query->innerJoin('users_roles', 'users_roles', 'users_roles.uid = users.uid');
    $query->innerJoin('role', 'role', 'role.rid = users_roles.rid');
    $query->condition('role.name', 'administrator');
    $query->fields('users', array('uid'));
    $rows = $query->execute();
    $admin_users_list = $rows->fetchAll();

    foreach ($admin_users_list as $admin_user) {
      if (!in_array($admin_user->uid, $members)) {
        $entity->cmp_m_store[LANGUAGE_NONE][]['target_id'] = $admin_user->uid;
      }
    }
  }
}

Implement hook_module_implements_alter hook for our purpose, we should ensure two things:

  1. Make sure entity_insert runs last when hook_entity_insert is invoked.
  2. Make sure entity_presave runs last when hook_entity_presave is invoked.
/**
 * Implement hook_module_implements_alter().
 */
function custom_module_module_implements_alter(&$implementations, $hook) {
  // Testing with isset is only necessary if module doesn't implement the hook.
  if ($hook == 'entity_insert') {
    // Move our hook implementations to the bottom.
    $group = $implementations['custom_module'];
    unset($implementations['custom_module']);
    $implementations['custom_module'] = $group;
  }
  if ($hook == 'entity_presave') {
    // Move our hook implementations to the bottom.
    $group = $implementations['custom_module'];
    unset($implementations['custom_module']);
    $implementations['custom_module'] = $group;
  }
}

The above hook implementation helps to order the implementation in reverse order, using &$implementations we could get the module implementations done here in our example, say 'example_custom', which will be stored inside the $group variable. After we will unset all the implementations in 'custom_module', the re-assigns the implementations stored in '$group'.

Following this method, we could add members in commerce store with admin role.

We can help you further in Setting up and configuring Ubercart and Drupal Commerce in your Drupal Site. We offer a wide range of Drupal services to help you maintain and manage your Drupal websites. Get in touch with us to find more.