Problems with custom preprocessing function and template file from the views module

Events happening in the community are now at Drupal community events on www.drupal.org.
DrupalCuckoo's picture

hi,

I know the title doesn't say clearly what this discussion is all about but it's not easy to explain what I'm trying now for more than 5 hours (!!!) without any useful result :-(

Ok, let me explain:

I'm building a community-page. On each user-profile people will see - among other things - a view called "list_groups" (created with the views module) which lists all groups a user has joined. It's a normal HTML list which contains the node titles as links.

Additionally, I'm developing a custom module for "content privacy". With this module a registered user will be able to decide who can see this "list_groups" view (Nobody, Only Friends, All User). This module is already working quite well, so this is not the thing where I'm stuck.

The problem is the view. On the views interface (admin/build/views/edit/list_groups) I've clicked the option "Theme", then clicked on "Display output" and saved the output to a file named "views-view--list-groups.tpl.php" to manipulate it.

I then wrapped the whole output with a php IF Statement (the spaces between "<", ">", "?" and "php" are made on purpose):

<?php
if($MYMODULE_show_group_list): ? >

/* in here is the theme output which was created by the views module */

<? php endif;
?>

What this does is quite simple: if $MYMODULE_show_group_list is set to "FALSE" the content of the template file (and therefore the group list) will not be rendered.

The variable $MYMODULE_show_group_list should come from a preprocessing function of my module. This function needs to compare the UID from the logged-in user (USER-A) and the UID from the user whose profile gets visited by the user who's logged in (USER-B). The preprocessing function is something like this

<?php
function MYMODULE_preprocess(&$variables) {

  
$variables['MYMODULE_show_group_list'] = FALSE;

  
$account_uid = $variables['account']->uid; // <-- uid of the user whose profile is beeing viewed
  
$user_uid = $variables['user']->uid; // <-- uid of the user who's logged in

  
switch($condition) {
      
      case
0:
        
$variables['MYMODULE_show_group_list'] = FALSE;
         break;

      case
1:
        
$count_relations = user_relationships_load(array("between"=>array($user_uid, $account_uid), "approved"=>1), array("count"=>TRUE));
         if(
$count_relations > 0) {
           
$variables['MYMODULE_show_group_list'] = TRUE;
         } else {
           
$variables['MYMODULE_show_group_list'] = FALSE;
         }
         break;
            
      case
2:
        
$variables['MYMODULE_show_group_list'] = TRUE;
         break;
   }
}
?>

In short: the $condition variable in the switch statement is a value between 0 and 2 which comes from the database. This is working properly and can be ignored. The main problem is that I can't get a proper $account_uid. This UID belongs to USER-B. I need this to compare it with the $user_uid which belongs to USER-A.

I'm not sure if the function is named correctly. For example, I tried a similar thing with the the Advanced-Profile-Kit module (APK) from the user Michelle. The APK module provides a panel called "author_pane". This panel provides a online/offline status to show if a user is logged in or not. My content-privacy-module has an option to hide this online/offline status if a user wishes to (again with the three options Nobody, Only Friends, All User). But this time I named the preprocess function MYMODULE_preprocess_author_pane and it's working correctly (but don't ask me why!).

So, how can I get information about the visited profile page in my preprocessing function (UID would be enough) and how do I have to name this function correctly?

Thanks for any help.

best regards.

Comments

global $user

jwolf's picture

I think you might be missing global $user in your function:

function MODULE_or_THEME_NAME_preprocess(&$vars) {
  global $user;
  // your magic here...
}

Hi jwolf, could you please

DrupalCuckoo's picture

Hi jwolf,

could you please explain me why I need to reference $user in my preprocess function since I'm not using $user at all?

thanks for your help.

best regards.

a proper $account_uid

jwolf's picture

The main problem is that I can't get a proper $account_uid

Try this:

<?php
function MODULE_or_THEME_NAME_preprocess(&$vars) {
  if (
$vars['profile']) {
   
$account_uid = $vars['account']->uid;
  }
}
?>

or you could use preprocess_user_profile() :

<?php
function MODULE_or_THEME_NAME_preprocess_user_profile(&$vars) {
 
$account_uid = $vars['account']->uid;
}
?>

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: