Some modules add sections to your 'My Account' page. For example, the user module adds content like 'Member for: 4 days.' Blog module, profile module as well as many contributed modules add in their own content. Some modules provide a configuration option to decide if such content is shown, or indeed, in which order these are shown, but many do not. For modules that don’t include this option, we need to write a very simple custom module in order to hide or alter the position of this information from the ‘My account’ page. The thought of designing a custom module may seem daunting at first, but it is actually a very simple process. The following tutorial will walk you through all the necessary steps to build and implement such a module.

The Devel Module

Before we start building the module, we need to install a Drupal developer module – Devel. The Devel module can be downloaded from the Drupal Devel project page: http://drupal.org/project/devel. Download the latest stable version of the module (at the time of writing 6.x-1.17). Once you have downloaded and unzipped the folder, move it to your Drupal modules folder (examplesite/sites/all/modules).

Now log into your Drupal site and head over to the modules page to enable the Devel module (administer/site building/modules). Scroll down the list of modules until you find the Devel module, which will be listed under the development section. There are a total of 5 modules that come with the Devel package, but for the purposes of this tutorial, we only need to concern ourselves with the Devel module itself. Go ahead and check the Devel module’s enable check box to the left of the module, scroll down to the bottom of the page and hit ‘Save Configuration’ to enable the module.

We could configure the module on the Devel Settings page, but as we only need the most basic of functionality, we can use Devel straight out of the box with zero configuration. With the Devel module enabled, head over to the ‘My account’ page which can be accessed from the site admin menu (My account is usually the first item on the admin menu).

At the top of the ‘My account’ page you will see the usual ‘view’ and ‘edit’ tabs. However beside these, the Devel module has added two new tabs ‘dev load’ and ‘dev render’. Completely ignore the ‘dev load’ tab as we will not be using this. Clicking on the dev render tab will bring up a box with a ‘Krumo version 0.2a…’ signature in the bottom right hand corner. In the top left hand corner, you should see some text that looks something like this: …(Array, 2 elements). Click on the text and the box should unfold to reveal more values. In the interest of keeping this tutorial as simple as possible, I am going to avoid a discussion on PHP arrays. You can find information on the subject here: http://w3schools.com/php/php_arrays.asp.

At this stage, all you need to know is that we are going to use these values to help us hide those unwanted elements on your ‘My account’ page such as ‘History’ and ‘view recent blog items’. Leave the My Account page open for now as we will come back to it after we have created the files for our custom module.

Building the custom module

Building a custom module for Drupal is a lot easier than it sounds. Our first step is to create a new folder. Head over to examplesite/sites/all/modules and create a new folder and name it ‘custom’. This is where our custom module will live. Open the custom folder up and create another folder and name it ‘profile_alter’. This will be the name of our custom module.

Our next step is to create a .info file (which is just a plain text file) that will supply Drupal with information about our custom module. Create a new file using your text editor – (Notepad on Windows, Text Edit on the Mac) and paste the following into it:

name = Profile Alter
description = Allows you to hide elements on your My account page i.e. History.
core = 6.x
package = Custom
php = 5.1

Now save the file as profile_alter.info and place it in the profile_alter folder we created earlier. Here’s a breakdown of each element in our .info file:

name – is the name of our module. Drupal will read this and display the name of our module in the Modules Administration section of our site.

description – is a description of what our module does. Again, Drupal reads this and displays the description beside our module’s name in the module admin section of our site.

core - is the version of Drupal the module is intended to be used with. We are using version 6, so we have set this to version 6.x which means it can be used with any of the version 6 updates i.e. 6.1, 6.2 etc.

package - tells Drupal which group to store your module under in the modules admin section of your site. For example, the out-of-the-box blog module is listed under the ‘core-optional’ group. Here, we are creating a new group named ‘Custom’ to store our module under.

php – tells Drupal which version of PHP our module is written in. We are using version 5.1.

With the profile_alter.info file written and saved in our profile_alter module folder, we are now ready to create the module file itself. Create a new file using your text editor and paste the following code into it:

<?php
/**
 * @file
 * Module that allows you to hide elements on the
 * My Account Page. For example, you can hide users'
 * history. 
 */

/**
 * Implementation of hook_profile_alter()
 */
function profile_alter_profile_alter(&$account) {
  unset($account->content[' '][' ']);
}

Save the file as profile_alter.module and place it in the profile_alter folder we created earlier alongside the profile_alter.info file. We now have all the files required for our module to work. The above code lays out the basic framework for our module, we just have to add specific pieces of information that will allow us to target the elements we wish to hide on the ‘My account’ page of our site. This is where the Devel module we installed earlier comes in.

Head back over to your ‘My account’ page and click on the Dev Render tab at the top of the page which will bring up the Devel box again. Click on the …(Array, x amount of elements) text in the top left hand corner of the Devel box to unfold the box. The amount of Arrays contained in the box (The words written in bold i.e. summary), depends on what modules you have installed. For this tutorial, I am using an out-of-the-box Drupal 6.13 install with the blog module enabled. The Devel box is therefore displaying 2 Arrays:

summary(Array, 6 elements) which means summary is an array containing 6 elements

user_picture(Array, 2 elements) which means user_picture is an array containing 2 elements.

Since I don’t have a user picture for my users’ profiles, I am only going to concern myself with the summary array. Clicking on the summary array will unfold it, revealing it’s 6 elements. The 6 elements are as follows:

blog (Array, 4 elements)
#type (String, 21 characters ) user_profile_category
#attributes (Array, 1 element)
#weight (Integer) 5
#title (String, 7 characters ) History
member_for (Array, 3 elements)

Armed with this information, we are now ready to hide the elements on our ‘My account’ page using our custom module. For the purposes of this tutorial, I am going to hide the History Member for x weeks x days, and Blog View recent blog entries sections of the ‘My account’ page.

Head back over to the profile_alter module folder that we created earlier and open up the profile_alter.module file that contains our module code. Locate the following section of code (line 14) in the file:

function profile_alter_profile_alter(&$account) {
  unset($account->content[' '][' ']);
}

Specifically, we will be focusing on this section of the code:
content[' '][' ']);

To hide the Blog section of your ‘My account’ page, take the following steps:

1. Type the name of our first array ‘summary’, which we found on the ‘My account’ page using the Devel module, between the first set of brackets like so:

content['summary'][' ']);

The array summary contains the element ‘blog’ that we need to target in order to hide the blog section on the ‘My account’ page, so it goes in between the first set of brackets. We can view summary as a kind of parent of the ‘blog’ element.

2. Type the blog element into the second set of brackets like so:

content['summary ']['blog']);

Our code should now look like this:

function profile_alter_profile_alter(&$account) {
  unset($account->content['summary']['blog']);
}

We now want to hide the History section of the ‘My account’ page so we need to add another line of code to our function in order to target this section. This line of code is exactly the same as the code we used to target the blog section. Our code should now look like this:

function profile_alter_profile_alter(&$account) {
  unset($account->content['summary ']['blog ']);
  unset($account->content[' '][' ']);
}

As with targeting the blog section, we will want to place the parent array ‘summary’ in the first set of brackets and it’s element ‘#title’, (by using the Devel module, we were able to ascertain that the #title element displays the History title on the ‘My account’ page) in the second set of brackets. The code should now look like this:

function profile_alter_profile_alter(&$account) {
  unset($account->content['summary']['blog']);
  unset($account->content['summary']['#title']);
} 

However, our new line of code is only hiding the ‘History’ title of the ‘My account’ page so we will need to add another line of code to our function in order to remove the member for x weeks x days section. Going back to our Devel box, we can see that this section can be targeted by using the member_for element of the summary array. Go ahead and add another unset line of code to the function, and place summary and member_for in the first and second set of brackets respectively. This is the last step and your code should now look like this in its entirety:

<?php
/**
 * @file
 * Module that allows you to hide elements on the
 * My Account Page. For example, you can hide user's
 * history. 
 */

/**
 * Implementation of hook_profile_alter()
 */

function profile_alter_profile_alter(&$account) {
  unset($account->content['summary']['blog']);
  unset($account->content['summary']['#title']);
  unset($account->content['summary']['member_for']);
}

Go ahead and save the profile_alter.module file if you haven’t already done so. If there are other sections of the ‘My account’ page that you wish to hide, you can use the Devel box to find the specific array and it’s elements that target that section, and follow the above procedure to hide them. Add as many duplicate lines of the
unset($account->content[‘ ’][‘ ’]); code format as needed to hide the element.

At this point, we need to activate our custom module in Drupal to ensure it is doing what it should do – hiding sections of our ‘My account’ page.

Enabling our custom module

Head over to the modules page to enable our custom module (administer/site building/modules). Scroll down the list of modules until you find our custom Profile Alter module, which will be listed under the Custom Modules section that we specified in the .info file. Go ahead and check the Profile Alter modules enable check box to the left of the module, scroll down to the bottom of the page and hit ‘Save Configuration’ to enable the module.

With the module enabled, head over to the ‘My account’ page. Both the History and Blog info sections of that page should now be hidden along with any other elements you targeted.

Reordering items on the my account page
In the case that you just want to reorder items on your ‘My account’ page (alter their weights) we take the following steps:

Open up the profile_alter.module file that we created in order to hide elements on our ‘My account’ page. Delete the code that is in there and replace it with the following code:

<?php
/**
 * @file
 * Module that allows you to alter the position of elements on the
 * My Account Page. For example, place the blog item above the member 
 * for item. 
 */

/**
 * Implementation of hook_profile_alter()
 */
 function profile_alter_profile_alter(&$account) {
  $account->content['']['']['#weight'] = '';
}

For the purpose of this tutorial, we are assuming that there are only two items on your my account page (Blog and Member for). We are going to place the ‘Blog View recent blog entries’ item on the ‘My account’ page above the ‘Member for x weeks x days’ item. The current order looks like this:

History

Member for
1 week 2 days
Blog
View recent blog entries

Our first step is to head over to the ‘My account’ page and select the ‘dev render’ tab to activate the Devel box. As in the previous example, click on the …(Array, 2 elements) text in the top left hand corner of the box to unfold it. ‘Summary’ is the parent array of the ‘Member for’ and ‘Blog’ items on our ‘My account’ page, so we are going to add that into the first set of brackets in our $account->content['']['']['#weight'] = ''; line of code in our profile_alter.module file.

Our code should now look like this:

$account->content[‘summary']['']['#weight'] = '';

We are going to target the ‘Blog’ item of our page. The ‘blog’ array is a child of the summary array, so this will go in our second set of brackets:

$account->content[‘summary']['blog']['#weight'] = '';
 

You will notice that I have already inserted the ‘#weight’ attribute into our third set of brackets. This is what Drupal uses to determine the weight of an item i.e. whether it goes below or above another item. In Drupal, items that are given a lighter weight i.e. a lower number such as zero, will be positioned at the top of a list. Items with a heavier weight, i.e. a higher number, will be positioned further down the list. This is demonstrated by the following example.

Item Weight
Peanuts 0
Eggs 1
Chips 2
Milk 3

Moving back to our blog item, it is safe to say that it has a heavier weight than our member for item as it is positioned after member for. There are only 2 items on our list, member for being the first, so therefore it will have a weight of zero which means blog has a weight of 1. By setting blog’s weight to -1, thus making it lighter than blog, it will be placed higher on the list. We need to add this value to our code as follows:

 $account->content[‘summary']['blog']['#weight'] = -1;

The code in our profile_alter.module file should now look like this in its entirety:

<?php
/**
 * @file
 * Module that allows you to alter the position of elements on the
 * My Account Page. For example, place the blog item above the member for
 * item. 
 */

/**
 * Implementation of hook_profile_alter()
 */
 function profile_alter_profile_alter(&$account) {
  $account->content['summary'] ['blog']['#weight'] = -1;
}

Go ahead and save that, and head over to your ‘My account’ page. You will notice that under the History section, the Blog item is now listed before the member for item.

If you have other items on your ‘My account’ page that you wish to reorder, you will need to use the Devel module to determine how to target those items and to find out what their weight is. As a quick example, let’s say I have the History section, (which has been the focus of this tutorial) and another section called ‘Personal Information’ which I’ve created in the Profiles section of the Drupal Admin interface - admin/user/profile. I want to place the Personal information section above the History section of the ‘My account’ page.

Going to the ‘My account’ page and clicking on the Devel dev render tab will bring up the Devel box displaying the item arrays. The History section of the ‘My account’ page can be targeted using the parent array ‘Summary’, and the Personal information section can be targeted using the parent array ‘Personal information’. If we open up the summary array and search for the #weight attribute, Devel will tell us it’s weight value. On my site, Summary has a weight of 5 (your value will no doubt be different depending on what is being displayed on your ‘My account’ page.). This tells us that if we wish to place the Personal information section above the history section of the ‘My account’ page, we will have to give it a weight of 4 or less.

I am not interested in reordering the children of these arrays (for example, member for and blog are children of the summary array). Therefore, I can solely target the parent array Personal information in my code, to place the personal information section above the History section on the ‘My account’ page. The code is as follows:

<?php
/**
 * @file
 * Module that allows you to alter the position of elements on the
 * My Account Page. For example, place the blog item above the member for
 * item. 
 */

/**
 * Implementation of hook_profile_alter()
 */
 function profile_alter_profile_alter(&$account) {
  $account->content['Personal information'] ['#weight'] = 4;
}

Further Reading

Pack Publishing Tutorial on creating a Drupal module
http://www.packtpub.com/article/creating-our-first-module-using-drupal6-...

PHP Arrays
http://w3schools.com/php/php_arrays.asp.

PHP Functions
http://w3schools.com/php/php_functions.asp

Drupal support page
http://drupal.org/support