Insert user friendly "click here to add your.." links when user profile fields are left blank

PLEASE NOTE! These snippets are user submitted. It is impossible to check them all, so please use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

Description

This php snippet displays a user friendly "click here to add your [profile field]" link when they are looking at their own profile page and have forgotten or not yet filled out some details.

The default settings with Drupal is to hide empty fields, so Users sometimes don't know they can add a field unless they look at the various EDIT MY ACCOUNT options.

The snippet checks to see if the person looking at the profile page is the same person or someone with ADMINISTER USERS permissions. It then checks to see if a field is not yet filled in and displays the "click here to add.." link if that's the case.

Dependencies: profile.module

Usage

  • For use in your user profile page override
  • Using a text editor like NOTEPAD.EXE or an equivalent, copy and paste the code into your user_profile.tpl.php file
  • Change the $profilecat value in the first line to match the profile category name you are using. In the example snippet we are using "Business Info."
  • Change the $profilefieldname value in the second line to match the profile field name you are using. In the example snippet we are using "profile_work_telephone"
  • Change the $linktext value in the second line to match the profile field name you are using. In the example snippet we are using "click to add your work phone number"
  • If you are using this snippet more than once in the same user_profile.tpl.php file add a number to the end of the $profilecat, $profilefieldname and $linktext titles each time you copy and use the snippet. e.g. $profilefieldname1, $linktext1, $profilefieldname2, $linktext2, $profilefieldname3, $linktext3 etc.
  • Tested and works with Drupal 4.5 and 4.6
  • Change the div class names or the link prefix text to suit.

<?php $profilecat = "Business Info."; ?>
<?php $profilefieldname = "profile_work_telephone"; ?>
<?php $linktext = "click to add your phone number"; ?>
<div class="fields">
<strong>Work Telephone Number:</strong>
<?php if ((user_access('administer users') || $GLOBALS['user']->uid == $user->uid) && (($user->$profilefieldname) == "")): ?>
<a href="/user/<?php print $user->uid ?>/edit/<?php print $profilecat ?>">
<?php print $linktext ?>
</a>
<?php endif; ?>
<?php print $user->$profilefieldname ?>
</div>

For Drupal 5:

- An "Edit this field" link is displayed even if the user has entered the information before. - The code is maybe more readable like this for beginners...

IMPORTANT: if you use this code more than one time on your profile page, add a number to each variable to distinguish them.

As you can see, the edit link has a class "edit".

<?php
// EDIT THESE VARIABLES TO SUIT YOUR NEEDS

$profilecat = "blog";
$profilefieldname = "profile_blog_title";
$linktext = "Edit this field";
$fieldlabel = "My field name :";
$pathedit = "user/$user->uid/edit/$profilecat";
$emptyfield = "No informations have been entered for this field. If you're the owner of this profile, you can              edit it";

print
'<div class="fields">';

print
'<h5>' . $fieldlabel . '</h5>';

if (isset(
$user->$profilefieldname)) {print $user->$profilefieldname;}

else {print
'<p>' . $emptyfield . '</p>';}

if ((
user_access('administer users') || $GLOBALS['user']->uid == $user->uid)) {

print
l($linktext, $pathedit, array('class' => 'edit'));

}

print
'</div>';
?>

If your field is a Web url, use this line

if (isset($user->$profilefieldname)) { print l($user->$profilefieldname, $user->$profilefieldname); }

Works Good, but also displays the "private" fields to all users

Sam308 - February 28, 2007 - 03:33

Using Drupal 4.7.6

(Issue 1) The script woks well but it displays the private fields if the private fields are included in the script.

I am currently using 33 profile fields in two different categories.

I placed all 33 profile fields into the script and noticed that "any" logged in user can see all the 33 fileds that are included in the script, including the private fields.

How can this script be modified to not display the private fields to "ALL" logged in users?

(Issue 2) Before I implemented this script, I was using the drupal core profile "Page title" feature (Administer » Settings » profiles) that turns a profile field item into a link so that users can view other user profiles which have the same profile field value in common. This feature is removed if you use the script.

Is it possible to restore this feature?

Add Link to Empty Profile Fields Utility Program
Since I had to expand the above script (12 lines of code) to accommodate 33 profile fields (396 lines of code), I created a Microsoft Excel utility that will automate the process of writing the PHP script. I am making this Utility available to others to make it easier to generate the script for multiple fields.

You can download it here: http://xlecom.com/downloads/Drupal_Add_Link_to_Empty_Profile_Fields.zip

Enjoy,

Sam Raheb (Sam308)

 
 

Drupal is a registered trademark of Dries Buytaert.