By theorichel on
Hello,
There are some fields in my profile that I want to keep private (IE: only visible for Admins and the user himself). I have overridden Template Php and created a user_profile.tpl.php, all according to the tips in the handbook. It works great apart from the fact that the setting of a field to 'Private' has no effect anymore. Should I change anything in Template.php or in the tpl.php.? I copy both below:
Many thanks
Theo Richel
The template.php
<?php
function themagrk_regions() {
return array(
'left' => t('left sidebar'),
'right1' => t('right sidebar1'),
'right2' => t('right sidebar2'),
'content' => t('content'),
'header' => t('header'),
'footer' => t('footer')
);
}
function themagrk_get_center_width($left, $right1, $right2) {
$width = 44;
if (!$left) {
$width = $width + 18;
}
if (!$right1) {
$width = $width + 19;
}
if (!$right2) {
$width = $width + 14;
}
return $width;
}
function phptemplate_menu_tree($pid = 1) {
if ($tree = menu_tree($pid)) {
$output .= "\n<ul>\n";
if (function_exists('base_path')) {
$base_path = base_path();
}
$url = $base_path . 'node';
if ($pid == 1) {
$output .= "<li><a href=\"" . $url . "\">home</a></li>";
};
$output .= $tree;
$output .= "\n</ul>\n";
return $output;
}
}
function phptemplate_menu_item($mid, $children = '', $leaf = TRUE) {
//return '<li class="'. ($leaf ? 'leaf' : ($children ? 'expanded' : 'collapsed')) .'">'. menu_item_link($mid) . $children ."</li>\n";
return '<li>'. menu_item_link($mid) . $children ."</li>\n";
}
function phptemplate_system_themes($form) {
foreach (element_children($form) as $key) {
$row = array();
if (is_array($form[$key]['description'])) {
$row[] = drupal_render($form[$key]['description']) . drupal_render($form[$key]['screenshot']) ;
$row[] = array('data' => drupal_render($form['status'][$key]), 'align' => 'center');
if ($form['theme_default']) {
$row[] = array('data' => drupal_render($form['theme_default'][$key]) . drupal_render($form[$key]['operations']), 'align' => 'center');
}
}
$rows[] = $row;
}
$header = array(t('Name/Screenshot'), t('Enabled'), t('Default'));
$output = theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
function phptemplate_system_user($form) {
foreach (element_children($form) as $key) {
$row = array();
if (is_array($form[$key]['description'])) {
$row[] = drupal_render($form[$key]['description']) . drupal_render($form[$key]['screenshot']);
$row[] = array('data' => drupal_render($form['theme'][$key]), 'align' => 'center');
}
$rows[] = $row;
}
$header = array(t('Name/Screenshot'), t('Selected'));
$output = theme('table', $header, $rows);
return $output;
}
function _phptemplate_variables($hook, $vars) {
if ($vars['node']->relatedcontent) {
$vars['template_file'] = "newsletter-teaser";
}
if($hook == 'page') {
themagrk_removetab('Web Links', $vars);
// add additional lines here to remove other tabs
}
return $vars;
}
function themagrk_removetab($label, &$vars) {
$tabs = explode("\n", $vars['tabs']);
$vars['tabs'] = '';
foreach($tabs as $tab) {
if(strpos($tab, '>' . $label . '<') === FALSE) {
$vars['tabs'] .= $tab . "\n";
}
}
}
/**
* Catch the theme_user_profile function, and redirect through the template api
*/
function phptemplate_user_profile($user, $fields = array()) {
// Pass to phptemplate, including translating the parameters to an associative array. The element names are the names that the variables
// will be assigned within your template.
/* potential need for other code to extract field info */
return _phptemplate_callback('user_profile', array('user' => $user, 'fields' => $fields));
}
?>And the user-profile.tpl.php is here:
<?php if($user->picture) {print theme('user_picture', $user);}?>
<div class="profile">
<div class="fields"><?php print check_plain($user->profile_mv); ?> <?php print check_plain($user->profile_tit); ?>
(<?php print check_plain ($user->profile_vrnm);?>)
<?php print check_plain($user->profile_achtrnm) ;?></div>
<div class="fields"><?php print check_plain($user->profile_pstadr) ;?></div>
<div class="fields"><?php print check_plain($user->profile_pstco); ?> <?php print check_plain($user->profile_plts) ;?></div>
<div class="fields"><?php print check_plain($user->profile_cntry); ?><br /><br /></div>
<div class="fields">Telefoonnr 1: <?php print check_plain($user->profile_tlf1) ;?></div>
<div class="fields">Telefoonnr 2: <?php print check_plain($user->profile_tlf2) ;?><br /></div>
<div class="fields">Website: <?php print check_plain($user->profile_url) ;?><br /><br /></div>
<div class="fields">Korte bio:<br /> <?php print check_markup($user->profile_bio); ?><br /><br /></div>
<div class="fields">Geregistreerd op deze site sinds: <?php print (format_date($user->created, 'small'))?> (<?php print (format_interval(time() - $user->created));?>)<br /><br /></div>
<?php if ($GLOBALS['user']->uid == $user->uid): /* check to see if it is the owner of the profile before displaying this link */ ?>
<div class="orders">
<?php print l(t('View your order history'), "store/history/$user->uid")?>
<?php endif; /* end of snippet */ ?> <br /><br /></div>
<div class="fields">Google AdSense client ID: <?php print check_plain($user->profile_google_adsense_client_id) ;?></div>
</div>
Comments
..
If particular fields are only available to ADMIN, i.e. when creating the profile field you set it to private, you can simply wrap the fields in a check to see if the global $users UID = 1 (admin)
example:
There are other ways of achieving the same result. that's just one example.
Alternatively, if you allow users to make specific fields private...such as their mobile phone number for example, check out this handbook page: http://drupal.org/node/45882
Thank you PhilK, I will
Try that out, but shouldnt it check for either the administrator OR the user whose profile it is? And what does the code look like then?
Sorry for thgese elementary questions.
Many thanks
yep. you're right.
here's the ammended code that checks to see if the person looking at the profile is that user or admin:
notes:
($GLOBALS['user']->uid == $user->uid)checks to see if the person viewing the profile is logged in as that person (User ID check).($GLOBALS['user']->uid == 1)checks to see if the person viewing the profile is logged in as the site admin (User ID check..i.e. if the USER ID = 1 that means it's admin).Alright and thanks
I pasted it in, got an error because of the { removed it and it worked again, but alas, the field profile_pstadr that I had set as 'private' is still displayed for an anonymous visitor. Here the code:
this would be good: <?php if
this would be good:
if (user_access('administer users')) { echo '<div class="fields">'. check_plain($user->profile_pstadr) .'</div>'; }..
you need that
{in there...and the closing<?php }?>for it to work.Hihaa!!!!
Gentlemen, thank you both very much for your time. It was the last advice that worked.
@PhilK: You are right about the { I probably made a mistake, though I do not know which, and after two days of bickering I do not really feel like finding out. You are two wonderful experts, thanks,
Theo Richel
Hi Theo, Can you post your
Hi Theo,
Can you post your solution here please?
Do you also know how I can get the "History stuff" at the bottom, or not showing at all?
Thanks a lot in advance,
greetings,
Martijn