Membership Status Block

Current Drupal CMS: 4.7.x (should work on later versions too)

The following describes a method for creating a Membership Status Block that lets logged-in site users know their current site / role status. If the user has not elected to become a site member, the Membership Status Block will also display a hyperlink to their profile area and a hyperlink to a member benefits page to encourage the user to become a site member.

You would want to place this Membership Status Block near the top of the site.

The following are two (2) Membership Status Block examples:

    Membership Status
    Selected: Member
    Currently: Member
    Membership Status
    Selected: Non-Member
    Currently: Pre-authorized
    Become a Member Now!

To understand the process, you need to know some preliminary information on how the site is set up.

In this case there are three (3) Roles and a Membership Profile Field set up by the site administrator. Keep in mind that Drupal also has a system default role called "authenticated user", so there are essentially a total of four (4) roles all together.

The site also contains a node page titled "Why You Should Sign-up and Become a Member". This page contains information that describes the benefits of becoming a member. In this example the url to this page is: ?q=node/21.

Drupal's Default System Role:

  • authenticated user - The role assigned to the user after the user clicks on the authentication link sent in the welcome email and logs into the site.

Roles set up by the site administrator:

  • pre-authorized - The role automatically assigned to the user by the system when the user initially signs up. These users will be able to login before their email address has been authenticated (non-authenticated users). Users will be removed from this role and assigned to the "authenticated user" role once they follow the link in their welcome email. This role is used in conjunction with the logintoboggan module.
  • member - A role "manually" assigned to preferred site members by the site administrator.
  • admin-level1 - a role assigned to alternative site administrators (not needed for this example, but is included in the PHP script below - can be removed).

Personal Information Profile Field (set up by the site administrator):

  • Membership: (* set to be a required field)

Membership Profile Field Options (set up by the site administrator):

  • Member
  • Non-Member

Below is the PHP Block code for the Membership Status Block:

<?php
global $user;
$output ='';
$approved_roles = array('pre-authorized', 'authenticated user', 'member', 'admin-level1');
if (is_array($user->roles)) {
  if (count(array_intersect($user->roles, $approved_roles)) > 0) {
      $sql = "SELECT value FROM profile_values WHERE (uid = $user->uid) AND (fid = 4)";
      $result = db_query($sql);
      $location = db_fetch_object($result);
      if (in_array('pre-authorized',$user->roles)) {$rolename = "Pre-authorized";}
      if (in_array('authenticated user',$user->roles)) {$rolename = "Non-Member";}
      if (in_array('member',$user->roles)) {$rolename = "Member";}
      if (in_array('admin-level1',$user->roles)) {$rolename = "admin-level1";}
      print ("<div class=item-list>Selected: <b>$location->value</b>");
      if ($rolename == "Pre-authorized") {print ("Currently: <a href=\"?q=node/21\"><b>$rolename</b></a>");} else {print ("Currently: <b>$rolename</b>");}
      if ($location->value == "Non-Member") {print ("<a href=\"?q=user/$user->uid/edit/Personal+Information\">Become a Member Now!</a></div>");} else {print ("</div>");}
  }
}
?>

Note: The information shown in the parentheses () below are comments and will not be displayed in the Membership Status Block.

The Membership Status Block Definition

    Membership Status (block title)
    Selected: Member (Membership Profile Field selection)
    Currently: Member (User Role)

The Membership Status Block code yields the following results for a possible outcome of six (6) different scenarios:

Membership Status
Selected: Non-Member (selected profile membership option)
Currently: Pre-authorized (the "Pre-authorized" text is a hyperlink to "Why You Should Sign-up and Become a Member" page)
Become a Member Now! (the "Become a Member Now!" text is a hyperlink to user's Personal Information section within their Profile so they can choose to become a Member via the Membership Profile Field)

Membership Status
Selected: Member
Currently: Pre-authorized (the "Pre-authorized" text is a hyperlink to "Why You Should Sign-up and Become a Member" page)

Membership Status
Selected: Non-Member (selected profile membership option)
Currently: Non-Member (authenticated user)
Become a Member Now! (the "Become a Member Now!" text is a hyperlink to user's Personal Information section within their Profile so they can choose to become a Member via the Membership Profile Field)

Membership Status
Selected: Member (selected profile membership option)
Currently: Non-Member (authenticated user)

Membership Status
Selected: Member
Currently: Member

Note: This following situation only occurs if the user changes their membership profile field to "Non-Member" after the site administrator has already manually assigned the user the "member" role.

Membership Status
Selected: Non-Member
Currently: Member
Become a Member Now! (the "Become a Member Now!" text is a hyperlink to user's Personal Information section within their Profile so they can choose to become a Member via the Membership Profile Field)

Enjoy,

Sam Raheb (Sam308)