Link from Gallery2 photo to Drupal User Profile

macco - June 28, 2007 - 01:04
Project:Gallery
Version:5.x-1.0
Component:Miscellaneous
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Hello,
has anybody managed to link from an album/photo to the user profil?
I tried to include a link in photo.tpl that points to the user profil. I can't
acces Drupals uid in embedded Gallery.
I know that the externalId/uid is stored in a table called g3_ExternalIdMap,
but how can I acces this table from photo.tpl?

Thanks in advance,

Marco

#1

profix898 - June 28, 2007 - 12:37

Its quite easy to achieve what you want. Open the theme.inc file of your theme. If you need an additional variable for a photo page, search for the 'showPhotoPage(&$template, $item, $params) { ... }' function. As the $user object of Drupal is a global variable you can easily access it directly (even outside Drupal, e.g. in G2). You can now get and modify all available theme variables using $theme =& $template->getVariableByReference('theme');. In case you need a new template variable 'drupaluser' containing the Drupal username then you would use the following code:

<?php
function showPhotoPage(&$template, $item, $params) {
   
$dataTypes = array('owner', 'parents', 'systemLinks', 'itemLinks', 'permissions',
              
'itemLinksDetailed', 'itemNavigator', 'imageViews');
    if (!empty(
$params['showMicroThumbs'])) {
       
$dataTypes[] = 'navThumbnails';
    }
   
$ret = $this->loadCommonTemplateData($template, $item, $params, $dataTypes);
    if (
$ret) {
        return array(
$ret, null);
    }
   
       
// -- begin: add this -- //
   
global $user;
   
$theme =& $template->getVariableByReference('theme');
   
$theme['drupaluser'] = $user->name;
       
// -- end: add this -- //

   
return array(null, 'theme.tpl');
    }
?>

In your .tpl file you can then access the 'drupaluser' variable like any other predefined variable. If you e.g. want to welcome the user with its Drupal username, you would add the following line to your template Welcome {$theme.drupaluser}!.

Hope you can follow this short tutorial. Let me know if you need further assistance.

#2

macco - June 28, 2007 - 17:44

Thanks,
you put me on the right track.
But I wanted the Drupal ID from the owner of the photo to link to the owners profil.
I apologize for my incomprehensible writing
I did this way now:

<?php
function showPhotoPage(&$template, $item, $params) {
   
$dataTypes = array('owner', 'parents', 'systemLinks', 'itemLinks', 'permissions',
              
'itemLinksDetailed', 'itemNavigator', 'imageViews');
    if (!empty(
$params['showMicroThumbs'])) {
       
$dataTypes[] = 'navThumbnails';
    }
   
$ret = $this->loadCommonTemplateData($template, $item, $params, $dataTypes);
    if (
$ret) {
        return array(
$ret, null);
    }        

       
// -- begin: add this -- //   
   
         
global $gallery;
   
$theme =& $template->getVariableByReference('theme');   
   
$query = "SELECT g_externalId  FROM g2_ExternalIdMap WHERE g_entityId
='"
.$theme['item']['owner']['id']."' AND g_entityType='GalleryUser'";
   
$results = $gallery->search($query);
      
$drupaluserId = $results[1]->nextResult();
   
$theme['drupaluserId'] = $drupaluserId[0];

   
// -- end: add this -- //

   
return array(null, 'theme.tpl');
    }
?>

I think your tutorial will help me in the future.

By the way, is there a way to use Drupal functions in Gallery2?

Danke,
Marco

#3

macco - June 29, 2007 - 09:31
Status:active» fixed

I close this issue.
Thanks again to profix898.

Regard,
Marco

#4

scott summers - July 13, 2007 - 08:19

Sorry i didn't work for me. i tried both the patches but neither $theme['drupaluser'] nor $theme['drupaluserId'] could be accessed in my modules/core/templates/blocks/local/ItemInfo.tpl file. It seems that im missing something.

#5

macco - July 13, 2007 - 08:48
Status:fixed» active

Hi,
it seems to me that you placed the code at a wrong location.

In theme.inc should be a part for modules, something like this:

<?php
/**
     * @see GalleryTheme::showModulePage
     */
   
function showModulePage(&$template, $item, $params, $templateFile) {
   
$ret = $this->loadCommonTemplateData(
       
$template, $item, $params, array('parents', 'systemLinks'));
    if (
$ret) {
        return array(
$ret, null);
    }
   
          
// -- begin: add this -- //   
   
         
global $gallery;
         
$theme =& $template->getVariableByReference('theme');   
         
$query = "SELECT g_externalId  FROM g2_ExternalIdMap WHERE g_entityId
          ='"
.$theme['item']['owner']['id']."' AND g_entityType='GalleryUser'";
         
$results = $gallery->search($query);
         
$drupaluserId = $results[1]->nextResult();
         
$theme['drupaluserId'] = $drupaluserId[0];

         
// -- end: add this -- //

   
return array(null, 'theme.tpl');
    }
?>

If this doesn't help, please post exactly what you have done including your code.

Marco

#6

scott summers - July 14, 2007 - 12:14

Hi macco,
I've added the following code at location: www.mysite.com/gallery2/themes/matrix/theme.inc file.
I tried to use it in modules/core/templates/blocks/local/ItemInfo.tpl file the same way profix898 has suggested i.e. Welcome {$theme.drupaluser}!. but outputs only Welcome !.

<?php
function showPhotoPage(&$template, $item, $params) {
   
$dataTypes = array('owner', 'parents', 'systemLinks', 'itemLinks', 'permissions',
              
'itemLinksDetailed', 'itemNavigator', 'imageViews');
    if (!empty(
$params['showMicroThumbs'])) {
       
$dataTypes[] = 'navThumbnails';
    }
   
$ret = $this->loadCommonTemplateData($template, $item, $params, $dataTypes);
    if (
$ret) {
        return array(
$ret->wrap(__FILE__, __LINE__), null);
    }
       
// -- begin: add this -- //
         
global $gallery;
   
$theme =& $template->getVariableByReference('theme');  
   
$query = "SELECT g_externalId  FROM g2_ExternalIdMap WHERE g_entityId='".$theme['item']['owner']['id']."' AND g_entityType='GalleryUser'";
   
$results = $gallery->search($query);
      
$drupaluserId = $results[1]->nextResult();
   
$theme['drupaluserId'] = $drupaluserId[0];
       
// -- end: add this -- //

   
return array(null, 'theme.tpl');
}
?>

Thanks for your kind help.
im new to drupal but appreciate the way people on drupal help others. thanks to drupal and all drupal guys.

I.X.

#7

scott summers - July 14, 2007 - 12:16

sorry i forgot this to ask. i have heard about contact owner module for gallery. but could not find it. could you please tell me the url or where to find this module for gallery 2.1. i shall be very thankful.

#8

macco - July 14, 2007 - 13:46

Sorry don't know about blocks.
But you have the function that gives the information to ItemInfo.tpl. That is not theme.inc I guess it should be somewhere in modules/core, I am afraid you have to search a bit.

#9

profix898 - July 15, 2007 - 21:55

i have heard about contact owner module for gallery. but could not find it

Take a look at the user contributed modules (http://codex.gallery2.org/Gallery2:User_Contributions) the contact owner module is listed there as well ...

I've added the following code at location: www.mysite.com/gallery2/themes/matrix/theme.inc file.
I tried to use it in modules/core/templates/blocks/local/ItemInfo.tpl file the same way profix898 has suggested i.e. Welcome {$theme.drupaluser}!. but outputs only Welcome !.

Its a little more difficult in your case. Blocks are reusable templates that are embedded into other (photo/album) templates. Without telling you too much details, this is what you need to make it work ...

Make the modification to theme.inc as described above (and as you have already tried). Then open photo.tpl (in the templates folder of your theme, e.g. matrix/templates/photo.tpl) and search for a snippet like this: {g->block type="core.ItemInfo" ... }. That's the code which includes the block template, in this case the 'core.ItemInfo' block. The template is located at modules/core/templates/blocks/ItemInfo.tpl. You must somehow pass the data into your blocks template now and thats what was missing in your attempts to get this working, I guess. The easiest way to achieve this is to add an additional element to the parameter list, that is passed to the block. You can then access this parameter in your block template.

Not sure you was able to follow me through the above paragraph!? If not, forget about the theory and take a look at the actual code. Here are the modifications needed to make this work:

1. /themes/matrix/theme.inc:
Take the code as posted above. No further modifications needed.

2. /themes/matrix/templates/photo.tpl:
In this file you search for a section that starts like {g->block type="core.ItemInfo" ... } and change it to this

<td style="width: 30%">
  {g->block type="core.ItemInfo"
            item=$theme.item
            showDate=true
            showOwner=$theme.params.showImageOwner
            class="giInfo"
            drupaluser=$theme.drupaluser} // This passes $theme.drupaluser to ItemInfo.tpl as 'drupaluser'
  {g->block type="core.PhotoSizes" class="giInfo"}
</td>

3. /modules/core/templates/blocks/ItemInfo.tpl
You can now use the new variable like this Welcome {$drupaluser}! in your block template.

To sum up: You first introduce a new theme variable in theme.inc, that will be available for all theme templates such as photo.tpl. You then pass the variable into you block template (ItemInfo.tpl) by adding it to the parameter list of the {g->block ... } call.

As blocks are reusable in several locations, you will notice that the variable will not be available in every case. You will need to modify album.tpl in addition to photo.tpl to make it available for the block on album pages. For this you must search album.tpl for the code that includes the block and add the parameter there as well.
I also suggest that you wrap your code in ItemInfo.tpl like this

{if !empty($drupaluser)}
  Welcome {$drupaluser}!
{/if}

to make sure it is only displayed when the variable is actually set.

Hope you got it. Let me know if you need additional help.

#10

scott summers - August 7, 2007 - 06:47
Status:active» fixed

GREAT!!!! profix898, it worked very well for me. Many Thanks again.

Sorry for such a long delay. Actually i was off for days.

Anyway once again many thanks to you and macco for your assistance on this issue. Following your instructions i have also made the username with each thumbnail to link user's drupal profile.

I'm posting the code below if it can be helpful to other users.

ALBUM PAGE CODE

<?php

   
/**
     * @see GalleryTheme::showAlbumPage
     */
   
function showAlbumPage(&$template, $item, $params, $childIds) {
   
$ret = $this->loadCommonTemplateData(
       
$template, $item, $params,
        array(
'owner', 'viewCount', 'childCount', 'descendentCount', 'parents',
         
'systemLinks', 'itemLinks', 'itemSummaries', 'permissions',
         
'thumbnails', 'pageNavigator', 'jumpRange'),
        
$childIds);
    if (
$ret) {
        return array(
$ret->wrap(__FILE__, __LINE__), null);
    }

    global
$user;   
    if(
$user->uid) {
           
// -- begin: add this -- //
             
global $gallery;
       
$theme =& $template->getVariableByReference('theme');
       
// drupal id of the album owner
       
$query = "SELECT g_externalId FROM g2_ExternalIdMap WHERE g_entityId='".$theme['item']['owner']['id']."' AND g_entityType='GalleryUser'";
       
$results = $gallery->search($query);
       
$drupalId = $results[1]->nextResult();
       
$theme['drupaluserId'] = $drupalId[0];
       
       
// drupal ids for each thumbnail
       
$count_thumbnails = $theme['item']['childCount'];
       
$thumb_ids = '(';
        for(
$i=0;$i<$count_thumbnails;$i++) {
            if(!empty(
$theme['children'][$i]['ownerId']))
               
$thumb_ids .= $theme['children'][$i]['ownerId'].',';
            else break;   
        }
       
$thumb_ids = substr($thumb_ids,0,-1).')';
       
$query = "SELECT g_externalId, g_entityId FROM g2_ExternalIdMap WHERE g_entityId IN ".$thumb_ids." AND g_entityType='GalleryUser'";
       
$result = mysql_query($query);
        while(
$row = mysql_fetch_object($result)) {
           
$theme['drupalId_thmb'][$row->g_entityId] = $row->g_externalId;
        }
           
// -- end: add this -- //
   
}
   
    return array(
null, 'theme.tpl');
    }
?>

PHOTO PAGE CODE

<?php

   
/**
     * @see GalleryTheme::showPhotoPage
     */
   
function showPhotoPage(&$template, $item, $params) {
   
$dataTypes = array('owner', 'parents', 'systemLinks', 'itemLinks', 'permissions',
              
'itemLinksDetailed', 'itemNavigator', 'imageViews');
    if (!empty(
$params['showMicroThumbs'])) {
       
$dataTypes[] = 'navThumbnails';
    }
   
$ret = $this->loadCommonTemplateData($template, $item, $params, $dataTypes);
    if (
$ret) {
        return array(
$ret->wrap(__FILE__, __LINE__), null);
    }
   
    global
$user;   
    if(
$user->uid) {
       
// -- begin: add this -- //
         
global $gallery;
       
$theme =& $template->getVariableByReference('theme'); 
       
$query = "SELECT g_externalId  FROM g2_ExternalIdMap WHERE g_entityId='".$theme['item']['owner']['id']."' AND g_entityType='GalleryUser'";
       
$results = $gallery->search($query);
       
$drupalId = $results[1]->nextResult();
       
$theme['drupaluserId'] = $drupalId[0];
       
// -- end: add this -- //
   
}   

    return array(
null, 'theme.tpl');
    }
?>

Once agian Thanks for your help.

I.X.

#11

yngens - August 7, 2007 - 08:19

subscribe

#12

Anonymous - August 21, 2007 - 08:20
Status:fixed» closed

#13

plan9 - May 28, 2009 - 22:56
Component:User interface / Theming» Miscellaneous

Does this method still work with the latest version of gallery 2.3?
I'm using Ice theme.

Thanks for any info

G

#14

plan9 - June 1, 2009 - 21:20
Title:Link from Gallery2 photo to Drupal User Profil» Link from Gallery2 photo to Drupal User Profile
Status:closed» active

I've also tried the above with Matrix and it still doesn't work. I'm assuming therefore that the above method is incompatible with gallery 2.3

Can anyone else please verify?

Thanks

G

#15

plan9 - August 13, 2009 - 00:04

Any chance of an update for gallery 2.3 please?

#16

plan9 - October 2, 2009 - 17:10

Hello...

Anyone....?

#17

plan9 - November 12, 2009 - 07:22
Status:active» closed

Closing this issues as gallery module is now dead

 
 

Drupal is a registered trademark of Dries Buytaert.