Community Documentation

Use case: Custom-tailored Organic Groups breadcrumbs (deprecated)

Last updated June 3, 2012. Created by donquixote on January 11, 2012.
Log in to edit this page.

Deprecated
Since the 7.x-1.2, this is much easier to achieve with the "Node parent" system, with token support.


Note for D6 users: The idea is quite similar, but API details are a bit different.

On a site with Organic Groups installed, you create those tabs for a group node:

node/[group nid] - the group start page, with timeline / activity stream.
node/[group nid]/about - group description, location info, etc.
node/[group nid]/events - list of group events
node/[group nid]/forum - list of group discussions

The */events and */forum might be implemented as panel variants, views pages with node argument, or as custom hook_menu() items.

Default Crumbs OG plugin

By default, the OG plugin for Crumbs gives you breadcrumbs like this:

For an event posted into a group:
- "Home"
- og "Groups" (list of groups)
- node/[group nid] "Amsterdam" (group node page)
- node/[event nid] "Beer meeting" (event node page)

For a discussion posted into a group:
- "Home"
- og "Groups" (list of groups)
- node/[group nid] "Amsterdam" (group node page)
- node/[event nid] "Anyone wanna swim in the port?" (discussion node page)

What you want instead

For an event posted into a group:
- "Home"
- og "Groups"
- node/[group nid] "Amsterdam"
- node/[group nid]/events "Events"
- node/[event nid] "Beer meeting"

And for discussions:
- "Home"
- og "Groups"
- node/[group nid] "Amsterdam"
- node/[group nid]/forum "Discussions"
- node/[event nid] "Anyone wanna swim in the port?"

Your custom plugin

How to achieve that with Crumbs 7.x (API preview):
(preview on the API that is coming in the 7.x branch. In current D6 it will look a little bit different)

; In mygroups.info
files[] = mygroups.crumbs.inc

<?php
// In mygroups.module
/**
* Implements hook_crumbs_plugins()
*/
function mygroups_crumbs_plugins($api) {
 
// We just want one plugin, with one crumbs rule.
  // Crumbs will instantiate the class by itself.
 
$api->monoPlugin();
}
?>

We cannot put this class directly into *.module, because crumbs_MonoPlugin might not be defined yet.
<?php
// In mygroups.crumbs.inc
/**
* Crumbs plugin.
* Adapted from the native crumbs plugin for og.
*
* We make this a "MonoPlugin", because findParent() always returns a string, not an array.
*/
class mygroups_CrumbsMonoPlugin implements crumbs_MonoPlugin {

 
/**
   * Information for the admin form.
   */
 
function describe($api) {
    return
t('Custom breadcrumbs for groups');
  }

  function
findParent__node_x($path, $item) {
   
$node = $item['map'][1];
    if (isset(
$node->group_audience['und'])) {
     
// Now we know this is a group post.
     
foreach ($node->group_audience['und'] as $group_info) {
       
// The node might be in several groups, but we just pick the first one..
       
$gid = $group_info['gid'];
       
// Attention: In D7, the group gid and the group node nid are different!
       
$row = db_query("SELECT * FROM {og} WHERE gid = :gid", array(':gid' => $gid))->fetchObject();
        if (
$row && $row->entity_type === 'node') {
         
$etid = $row->etid;
          switch (
$node->type) {
            case
'group_event':
              return
"node/$etid/events";
            case
'group_discussion':
              return
"node/$etid/forum";
          }
        }
      }
    }
  }
}
?>

In the crumbs configuration form

Your newly created plugin will probably already be enabled, as soon as Drupal knows about it (don't forget to flush the class and hook registries!)
But maybe there are other plugins that override it.

In the configuration form (admin/structure/crumbs), you will find your own plugin by the name "mygroups". You want to make sure that it comes before the default OG plugin.

In the end, your configuration (the "enabled" section) might look like this:

mygroups
og.*
menu.hierarchy.main-menu   - Main menu
*

--- disabled ---
[...]

The * does include anything that is in "inherit", including the default OG stuff ("og.*").

The menu.hierarchy.main-menu stuff is after mygroups and og.*, because if a group is linked in a menu, we still want it to have the regular groups breadcrumb, not something based on the menu hierarchy.

Isn't this still quite complicated, for one single breadcrumb item?

It looks like this should all be easier.
But, be honest, there is quite some logic involved here:
- This specific stuff should be only for group_discussion and group_event node types.
- It should only happen for group posts.

Maybe someone wants to write a UI module for popular use cases :)

Page status

About this page

Drupal version
Drupal 7.x
Audience
Programmers
Level
Advanced

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.