Theming Organic Groups Home Page Nodes

Last modified: August 27, 2009 - 02:51

I have seen a lot of posts by people looking to theme the OG home page. I myself have wanted the old style OG home page back from 4.7 for a long time, and I couldn't figure out any decent way to go about this. The documentation (at least that I have found) seems to really only tell you how to theme the actual group node type (node-og.tpl.php for example) or add in filters to the default og_ghp_ron theme. That is not what I'm going to explain.

Let's say you want to theme all of the posts that show up on your OG home pages. You want these posts to look different on the OG home page than on their regular full text and teaser listing pages. And you want all your group home pages to be the same. That's what I will explain (and if you want all your group pages to be different, then you can still use this code, or you can just use Views). I'm sure there's a better way to do this, but I eventually got frustrated hearing that this was easy but not hearing how to do it. So I just made my own way.

For the sake of this tutorial, we will assume your OG content type is called 'og'. If you don't know what yours is called, go to admin/og/og and click on "group home page". Whatever is selected there is your OG content type.

Also, you might want to back up your site before you do the tutorial just in case you aren't happy with the results.

I wanted a generic way to do this, and I didn't want to have to constantly update my node files when I added a new group. I also didn't want to rely on group creators to correctly make a path alias for the group. If you are the only person who creates groups, or you only have a couple groups, you can manually set your group path aliases on the group edit form to groups/the-title-of-your-group-goes-here and not install Pathauto.

So the first thing I did was install Pathauto. Pathauto allows you to create automatic user-friendly paths for any of your content. Once installed (follow the instructions in the Pathauto readme), go to the Pathauto settings page, click on "node path settings". If you ONLY want to add new aliases for your groups, then delete what's under "Default path pattern (applies to all node types with blank patterns below)" at the top of the node section. If you leave this filled in, all of your site content will get automatic aliases under that format. Pathauto is a very handy module, but I encourage you to read up on it or carefully look at the configuration page and think about your site if you leave that default pattern in place. So for this tutorial, feel free to delete it.

Now, in this same section, search out where it lists "Pattern for all og paths" (or whatever your OG content type is called). In that box, you will put: groups/[title-raw]

Then scroll down and check off the box that says "Bulk generate aliases for nodes that are not aliased". Then save. Now all of your group home pages should have an automatic and user-friendly alias.

Now that we have found a way to help our nodes determine if we're on a group home page or not, we're ready to edit our template file. Go into your theme folder and edit (or create) the file called node.tpl.php.

The only thing we are going to do differently than a regular node.tpl.php file with teaser/full options (see the other tutorials in this section) is check the Pathauto alias to see if we are on a group home page. However, we only want the first argument of the URL (the "groups" part), so we also have to trim down the alias we get back using php string functions. Below are a few code snippets of what I'm doing (scroll down further for the full/complete code).

Here is how we determine if we're on a group home page:

<?php
 
if (arg(0) == 'node' && is_numeric(arg(1))) {
   
$url_alias = drupal_get_path_alias('node/'.arg(1));
  } elseif (
arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
   
$url_alias = drupal_get_path_alias('taxonomy/term/'.arg(2));
  }

 
// We need to only take the first argument of the aliased path, so define the separator
 
$slash = '/';

 
// find out if the separator exists in the returned alias
 
$pos = strpos($url_alias, $slash);

 
// if the separator is not present, then just look at the URL alias; if it is present, strip the first argument only
 
if ($pos === false) {
 
$string = $url_alias;
  } else {
 
$string = substr($url_alias, 0, $pos);
  }
 
 
// if the first argument of the aliased path == 'groups', then we know it's a group home page
 
if ($string == 'groups'){ ?>

Then we will put our group home page node formatting after this. In my case, I just wanted a title link with who published it and when, so I used the following:

<?php if ($page == 0) { ?>
      <div class="content"><a href="<?php print $node_url; ?>"><?php print $title?></a><span class="submitted"><?php print " ".$user_name. " " . $date; ?></span></div>
    <?php }; ?>

After this we just provide the normal teaser view if the alias is not equal to 'groups':

<?php
 
// if the first argument of the aliased path wasn't 'groups', then it's a normal teaser view
 
}else{ ?>

    <div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
    <?php if ($picture) { print $picture; }?>
    <?php if ($page == 0) { ?>
      <h2 class="title"><a href="<?php print $node_url; ?>"><?php print $title?></a></h2>
    <?php }; ?>
    <span class="submitted"><?php print $submitted?></span> <span class="taxonomy"><?php print $terms?></span>
    <div class="content"><?php print $content?></div>
    <div class="clr">
    <?php if ($links): ?>
      <div class="links"><?php print $links; ?></div>
    <?php endif; ?>
    </div>
    </div>
  <?php } ?>

From there we go on to finish our node.tpl.php file with what the non-teaser view should look like.

The Full/Complete Code That You Can Use

You can edit the look/feel of what you want to customize.

<?php if ($teaser): ?>

  <?php
 
// we just get the drupal path args so that we can look up the path alias
 
if (arg(0) == 'node' && is_numeric(arg(1))) {
   
$url_alias = drupal_get_path_alias('node/'.arg(1));
  } elseif (
arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
   
$url_alias = drupal_get_path_alias('taxonomy/term/'.arg(2));
  }

 
// We need to only take the first argument of the aliased path, so define the separator
 
$slash = '/';

 
// find out if the separator exists in the returned alias
 
$pos = strpos($url_alias, $slash);

 
// if the separator is not present, then just look at the URL alias; if it is present, strip the first argument only
 
if ($pos === false) {
 
$string = $url_alias;
  } else {
 
$string = substr($url_alias, 0, $pos);
  }
 
 
// if the first argument of the aliased path == 'groups', then we know it's a group home page
 
if ($string == 'groups'){
 
// below this is where you need to put your node formatting for the og home page ?>


    <?php if ($page == 0) { ?>
      <div class="content"><a href="<?php print $node_url; ?>"><?php print $title?></a><span class="submitted"><?php print " ".$user_name. " " . $date; ?></span></div>
    <?php }; ?>
 
  <?php
 
// if the first argument of the aliased path wasn't 'groups', then it's a normal teaser view
 
}else{
 
// below this is where you need to put your normal node teaser formatting ?>

    <div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
    <?php if ($picture) { print $picture; }?>
    <?php if ($page == 0) { ?>
      <h2 class="title"><a href="<?php print $node_url; ?>"><?php print $title?></a></h2>
    <?php }; ?>
    <span class="submitted"><?php print $submitted?></span> <span class="taxonomy"><?php print $terms?></span>
    <div class="content"><?php print $content?></div>
    <div class="clr">
    <?php if ($links): ?>
      <div class="links"><?php print $links; ?></div>
    <?php endif; ?>
    </div>
    </div>
  <?php } ?>

<?php else:
// here is where your full node formatting goes ?>


<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?> clear-block">

<?php print $picture ?>

<?php if ($page == 0): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>

  <div class="meta">
  <?php if ($submitted): ?>
    <span class="submitted"><?php print $submitted ?></span>
  <?php endif; ?>

  <?php if ($terms): ?>
    <span class="terms"><?php print $terms ?></span>
  <?php endif;?>
  </div>

  <div class="content">
    <?php print $content ?>
  </div>

<?php
 
if ($links) {
    print
$links;
  }
?>


</div>

<?php endif; ?>

Hopefully that will help save somebody time.

Just wanted to post about my

torgosPizza - October 7, 2008 - 17:58

Just wanted to post about my method for doing this. You can pretty much replace the code from the first code block with this:

<?php
$type
= db_result(db_query("SELECT `type` FROM {node} WHERE `nid` = %d", arg(1)));

if (
$type == 'group') {  //  Change 'group' to whatever your group node type is called.
  // Group page formatting here.
?>

It's way more efficient than parsing a URL for a separator and using the args from there. I am also using this method, it should be noted, to get around the issue where your nodes (or group posts) are getting aliased to a directory within the main Group page. i.e.,

group page: example.com/mygroup
posted node: example.com/mygroup/mypost

The original posted code sees this example's first argument as being the same ("mygroup"), and so when you go to view the node itself, you are presented with the Group page themed node, instead of the full Node theme. So to get around it, I simply find out what the node type is of the page we're looking at; if it's a group page, theme the node as a group node, otherwise show the full node theme. Hope this helps someone else.

Why not just a template?

advseb - February 5, 2009 - 15:28

Hi,

why are you not using a template specific for the content type? For example, if your content type is named "grouphomepage", just create the following file in your theme's folder:

node-grouphomepage.tpl.php

Sebastian
--
http://fahrradtour-wandern-reisen.hpfsc.de/

The reason why this was done

torgosPizza - February 7, 2009 - 02:55

The reason why this was done is explained in the original post: "Let's say you want to theme all of the posts that show up on your OG home pages. You want these posts to look different on the OG home page than on their regular full text and teaser listing pages."

So for instance, on the "Group" page, I want to show a thumbnail and some custom text, links, etc. I can't easily do that just with a template, or a View (I tried both). The only way, at least that I found, was to use the snippet above to basically alter the way the node gets output depending on what page you are on.

I think you could potentially accomplish this same goal using a template, but you'd still need to put some conditions in there to see exactly what page you are on - Group page or Node page. I could be mistaken and I'll have to play around a bit to see if it can be done an easier way :)

Theme Information link

advseb - February 9, 2009 - 08:43

Hi,

the dynamic part of an OG homepage is done by the "river of news" view. If you open this view, there is a link "Theme Information". If you click on it you will be able to see how template files must be named to just target this single view output. However, changes done here will apply to all OG homepages, so if you need different looks this way won't work.

Sebastian
--
http://sebstein.hpfsc.de/

theme information

mailme.eva - June 16, 2009 - 09:11

i'm sorry, but my og_ghp_ron view does not seem to have this link anywhere.
can it be that this is something in v6?
if not, can you give me a hint where precisely (which section) to look for it?
thanks
e.

 
 

Drupal is a registered trademark of Dries Buytaert.