User login

Adding and modifying organic group action (create content) links

Moshe is a Drupal God. Look at this! There's a hook for everything!

Update for Drupal 6: Enter drupal_alter

<?php
    // Modify these links by reference. If you want control of the whole block, see og_block_details().
    drupal_alter('og_links', $links, $node);
?>

From og.modules own implementation of hook_og_block_details:

<?php
    $links = module_invoke_all('og_create_links', $node);

  // Modify these links by reference. If you want control of the whole block, see og_block_details().
  foreach (module_implements('og_link_alter') AS $module) {
    $function = $module .'_og_link_alter';
    $function($links, $node);
  }
?>

A create links hook AND an alter links hook! With the node object passed in for context! There aren't enough exclamation points in the digital universe to describe how cool this is.

<?php
// $group is an object containing the group node
function og_og_create_links($group) {
  $post_types = og_get_types('group_post');
  $types = node_get_types();
  foreach ($post_types as $post_type) {
    // We used to check for node_access(create) but then node admins would get a false positive and see node types that they could not create.
    // When this becomes a proper menu in D6, we get sorting for free
    $type_name = $types[$post_type]->name;
    if (module_invoke($types[$post_type]->module, 'access', 'create', $post_type)) {
      $links["create_$post_type"] = l(t('Create !type', array('!type' => $type_name)), "node/add/$post_type", array('title' => t('Add a new !s in this group.', array('!s' => $type_name))), "gids[]=$group->nid");
    }
  }
  return $links ? $links : array();
}
?>

These are the kind of links we need instead for project module integration with organic groups:

http://myagaric.com/node/add/project_issue/exampleproject/support
http://myagaric.com/node/add/project_issue/exampleproject/bug
http://myagaric.com/node/add/project_issue/exampleproject/feature

With the gids (group identification numbers) added.

We actually have made the fixes using project.module's hooks first (below— in another modification we used project module hooks to allow members of an organic group that is also a project node to assign issues to one another), but soon we'll do it with the organic groups module also (or use the hooks above to hide the mis-functioning links).

This like elsewhere is a first draft and should be showing up soon in a module near you.

<?php
/**
 * Implementation of project.module's hook_project_page_link_alter().
 *
 * Accepts a node context and an array of all links, which is passed by
 * reference so that changes made will be returned.
 */
function organic_project_project_page_link_alter($node, &$all_links) {
//  $all_links['support']['links']['request_support'] = 'booo';

  $proj = $node->uri;
  $pid = $node->nid;
  $proj_name = $node->title;

  $all_links['support']['links']['request_support'] = l(t('Request support'), 'node/add/project_issue/'. $proj .'/support', array('title' => t('Submit a support request for !s', array('!s' => $proj_name))), 'gids[]='. $pid);
  $all_links['support']['links']['report_bug'] = l(t('Report new bug'), 'node/add/project_issue/'. $proj . '/bug', array('title' => t('Submit a bug report for !s', array('!s' => $proj_name))), 'gids[]='. $pid);
  $all_links['support']['links']['request_feature'] = l(t('Request new feature'), 'node/add/project_issue/'. $proj . '/feature', array('title' => t('Submit new feature suggestion for !s', array('!s' => $proj_name))), 'gids[]='. $pid);
  // this is a new link
  $all_links['support']['links']['request_feature'] = l(  t('Make new task'), 'node/add/project_issue/'. $proj . '/task', array('title' => t('Submit a task for !s', array('!s' => $proj_name))), 'gids[]='. $pid);
 
  // this is also a new link
  $all_links['support']['links']['all_issues'] = l(t('View all issues'), 'project/issues/'. $proj, array('title' => t('View all issues for !s', array('!s' => $proj_name))), 'projects='. $pid .'&states=all');
}
?>

Correction: in the new link above to view all issues (regardless of status), a feature I really wish Drupal.org had, there was a hardcoded project ID. This has been replaced with $pid.

Update: As promised, here is the same integration for the organic groups block. Commented out last three lines is the wonderful test code that shows you I'm still not using a proper integrated development environment. It showed that the node had all the information identifying it as a project node and that $links was a keyed array of HTML representation of each link in the group block.

<?php
/**
 * Implementation of hook_og_link_alter().
 */
function organic_project_og_link_alter(&$links, $node) {
  if (!isset($node) || $node->type != 'project_project') {
    return;
  }
  $proj = $node->uri;
  $pid = $node->nid;
  $proj_name = $node->title;

  // capitalization of issue follows other create content links in OG block
  $links['create_project_issue'] = l(t('Create Issue'), 'node/add/project_issue/'. $proj, array('title' => t('Add an issue to the queue for !s', array('!s' => $proj_name))), 'gids[]='. $pid);
/*
drupal_set_message('called!');
drupal_set_message('<pre>'. var_export($node,TRUE) .'</pre>');
drupal_set_message('<pre>'. var_export($links,TRUE) .'</pre>');
*/
}
?>

Resolution

Searched words: 
og_links_alter now treats projects right (actually og_link_alter) drupal_alter

Comments

showing up in a program near you...

This is very cool, from the support discussions it looks like quite a few beginners such as myself are looking for a way to do this, but having to do every step from creating a new module to adding the output to a template is a little challenging... any chance you would put this in a walk-through/tutorial style post?

In case anyone else needs

In case anyone else needs this in the future the function is named: "hook_og_links_alter" Notice the 's' after link

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.