Creating an alias for each Profile2 instance in Drupal 7

June 18, 2021


Table of contents

Quick Access

Profile2 is a Drupal 7 module which allows the creation of multiple profile types, these profiles may be later assigned to roles via permissions. Profile2 includes a "Profile pages" module where profiles can be viewed and edited at their own page and get their own menu link, e.g. "My profile." Profile2 assigns a URL to each profile page that you create, and sometimes these URL’s might not be very user friendly. Nonetheless, what we can do to solve this issue is to assign an alias with Pathauto to a profile created by a user upon registration (or update) so that it can be later used to access a certain profile. In this case every user of our application is going to have a Profile2 called company (that is, every user is going to be the “owner” of a company). To achieve what I explained above we are going to be develop a custom module to create an alias to each Profile2 such that we can access each company individually by their name in a more user friendly way. Here is a little overview of what we need to do: 1. Determine the format we want the custom alias to look like. 2. Identify when a Profile2 is being created or edited. 3. Sanitize the data. 4. Check if the alias already exists. 5. Save the data so that the Profile2 can be accessed with its new alias. The format which we will be using for our alias will be as follows: /company/{company_name}. The main reasons why we will be using the company’s name as our “token” is because it is both user friendly and a requirement for every user of the application to give their company a unique name. Next, we will be using a hook which is given to us by the Profile2 API: “hook_profile2_presave”. This hook is invoked when a Profile2 is being inserted or updated to the database. To sanitize the alias we will use the Pathautho “pathauto_cleanstring” function and a custom [toAscii](http://cubiq.org/the-perfect-php-clean-url-generator/ "toAscii") function which will help us to create a clean URL. So let’s go ahead and implement this functionality: [prism:php] field_company_name['und']; $array = array_filter($array); if (!empty($array)) { $original_url = 'profile-' . $profile->type . '/' . $profile->uid; $profile_company_name = strtolower($profile->field_company_name['und'][0]['value']); // Convert to ACSII $profile_company_name = toAscii($profile_company_name); // Load Pathauto module if (module_load_include('inc','pathauto','pathauto') !== FALSE) { if (function_exists('pathauto_cleanstring')) { // Check if that alias already exists (the user might be editing his company) if (isset($profile->original)) { // Load old value $profile_old_value = $profile->original->field_company_name['und'][0]['value']; $profile_old_value = pathauto_cleanstring($profile_old_value); $old_url = 'company/' . $profile_old_value; $old_path = array( 'source' => $original_url, 'alias' => $old_url, 'language' => 'en' ); // Delete old alias path_delete($old_path); } $profile_company_name = pathauto_cleanstring($profile_company_name); $new_url = 'company/' . $profile_company_name; $path = array( 'source' => $original_url, 'alias' => $new_url, 'language' => 'en' ); // Save our newly created alias path_save($path); } } } } ?> [/prism:php] In the hook_profile2_presave function we first get the name of the company which we are currently processing. After we have both properly converted its name to ASCII and sanitized its data , we then proceed to check if that particular alias already exists. Since the company name has to be unique across all Profile2 companies, we can safely assume that if this alias already exists, it is because a company is being edited, therefore we proceed to delete it and insert its new value, otherwise we just go ahead and add an alias to the newly created company. In conclusion, we have created a short but powerful custom module which provides to us a way of accessing profiles of the Profile2 module in our application through an alias with Pathauto.