It'd be useful to be able to load a role by machine name, instead of having to manually search for it in other module code. The attached patch adds a role_export_load() function that returns a role by machine name.

Comments

deviantintegral’s picture

Status: Active » Needs review
StatusFileSize
new1.42 KB

Tada! Also, this patch statically caches role loading to save a few DB queries per request.

klausi’s picture

Status: Needs review » Needs work
+++ b/role_export.module
@@ -84,6 +84,28 @@ function role_export_role_name_exists($value) {
+ * @param $machine_name
+ *   The machine name of the role to load.
+ *
+ * @return
+ *   A role object containing the role ID and human-readable name, or FALSE if

Data types are missing in the docs, see http://drupal.org/node/1354#param

+++ b/role_export.module
@@ -84,6 +84,28 @@ function role_export_role_name_exists($value) {
+function role_export_load($machine_name) {
+  $roles = role_export_roles();
+
+  foreach ($roles as $role) {
+    if ($role->machine_name == $machine_name) {
+      return $role;
+    }

Instead of looping over all roles you should calculate the rid and access the roles array directly with rid as key.

deviantintegral’s picture

Status: Needs work » Needs review
StatusFileSize
new1.43 KB

Implements both of the suggestions from #2.

klausi’s picture

Status: Needs review » Needs work
+++ b/role_export.module
@@ -84,6 +84,27 @@ function role_export_role_name_exists($value) {
+ *
+ * @return stdClass
+ *   A role object containing the role ID and human-readable name, or FALSE if

the data type should be "object" in case of stdClass.

+++ b/role_export.module
@@ -84,6 +84,27 @@ function role_export_role_name_exists($value) {
+function role_export_load($machine_name) {
+  $roles = role_export_roles();
+
+  $rid = role_export_generate_id($machine_name);
+  if (isset($roles[$rid])) {
+    return $roles[$rid];
+  }
+
+  return FALSE;
+}

why do we need to load all roles here? Just calculate the rid and then call user_role_load()?

So I don't think we need the caching for role_export_roles().

Steven Brown’s picture

Issue summary: View changes
Status: Needs work » Closed (works as designed)

Since there has been no response to the issue for going forward and I agree with klausi in #4 comment, I am changing status.