background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

In a recent Drupal implementation, we used the Organic Groups module to allow users in a certain role to add content to group nodes. On the content type edit screens, for "Organic groups usage", we chose "Standard group post (typically only author may edit)". Unfortunately, this text is a little deceiving. The OG module grants group administrators the ability to edit any node in the group, which was undesired for our situation.

In the og.module module file, the function og_menu_alter() overrides the normal access control of a user's ability to edit nodes:

<?php
function og_menu_alter(&$menu) {
 
// If og_access is disabled, we at least add back the edit tab for group admins to edit their posts.
 
$menu['node/%node/edit']['access callback'] = 'og_menu_access_node_edit';
 
$menu['node/%node/edit']['access arguments'] = array(1);
}
?>

Prior to og_menu_alter() being executed, the menu structure was:

[access callback] => node_access
[access arguments] => Array
    (
        [0] => update
        [1] => 1
    )

The above array structure relies on the node_access() function to determine if a user has permission to edit a node. One solution to this problem is to define code in a module to reset this menu structure:

<?php
function MYMODULE_menu_alter(&$menu) {
 
$menu['node/%node/edit']['access callback'] = 'node_access';
 
$menu['node/%node/edit']['access arguments'] = array('update',1);
}
?>

Now, group administrators no longer have permission to edit every content item in a group.

Eric.London's picture

Here's a quick code snippet that shows you how you can create custom breadcrumbs for a certain node type. This code would reside in your template.php theme file.

<?php
function MYTHEME_preprocess_page(&$variables) {
  if (
$variables['node']->type == 'MYNODETYPE') {
       
   
$links = array();

   
// creating a link to the home page
   
$links[] = l('Home', '<front>');

   
// here's how you could add a link to a taxonomy page
   
$vid = 2;
    foreach (
$variables['node']->taxonomy as $k => $v) {
      if (
$v->vid = $vid) {  
       
$links[] = l($v->name, 'taxonomy/term/' . $v->tid);
        break;
      }
    }
       
   
// yet another link
   
$links[] = l('Some Other Link', 'SOMEOTHERLINK');

   
// lastly, overwrite the contents of the breadcrumbs variable in the page scope
   
$variables['breadcrumb'] = theme('breadcrumb', $links);
  }
}
?>

NOTE: if you were adding this code to a module, you could use the drupal_set_breadcrumb() function to do the same functionality.

In this code snippet I'll explain how to create a menu item that has a dynamic title.

<?php
// define hook_menu to create menu item
function MYMODULE_menu() {
 
$items = array();
   
 
$items['MY/PAGE/URL/%'] = array(
   
'page callback' => 'MY_PAGE_CALLBACK_FUNCTION',
   
'title callback' => 'MY_PAGE_CALLBACK_TITLE_FUNCTION',
   
'title arguments' => array(3),
   
'type' => MENU_CALLBACK,
  );

  return
$items;
}

// define title callback function
function MY_PAGE_CALLBACK_TITLE_FUNCTION($arg) {
  return
"My dynamic title: " . $arg;
}
?>

In the above example, if you browsed to the URL: MY/PAGE/URL/blah-blah-blah, you'd have a page title of: My dynamic title blah-blah-blah. In a more applicable example, you might need to pass the node id to the callback function and then return: $node->title;

Check out the documentation on wildcard loader arguments for more advanced options.

Today I wanted to add a unique identifier to each menu item in the primary links so I could theme each one individually. In this example, I added a incrementing class to each menu item so I could add an image via css. Here's a snippet of my theme function (found in template.php):

<?php
function MYTHEME_preprocess_page(&$variables) {
 
$linkCount = 0;
  foreach (
$variables['primary_links'] as $k => $v) {
   
$linkCount++;
   
$class = trim($v['attributes']['class'] . " nav$linkCount");
   
$variables['primary_links'][$k]['attributes']['class'] .= $class;
  }
}
?>

Eric.London's picture

When you create a menu, a block is automatically generated for you. Blocks can only be assigned to one region at a time. In some cases, you might want to have the same menu appear in your site in different places. This can be accomplished using the menu_tree function. This function accepts one argument, the parent ID of the menu. This ID can be found at admin/build/menu. If you mouseover the edit link of the menu you'd like to insert, you'll see the following URL structure: admin/build/menu/menu/edit/### (where ### represents the parent menu ID). You can now generate a menu using the following code:

<?php
print menu_tree('YOURMENUID');
?>

If you're having a hard time finding a menu ID, you could query the menu table [see below]. You'll be interested in the "pid" column (not the "mid" column, which is the unique identifier).

select * from {menu}

If you'd like to also show the menu's title, you could use menu_get_item to look it up:

<?php
$menuID
= 77;
$menuItem = menu_get_item($menuID);
if (
$menuItem) {
  print
"<h3>" . $menuItem['title'] . "</h3>";
  print
menu_tree($menuID);
}                                                                                                                                                           
?>

Syndicate content