Some paths use node/%node for project nodes (e.g. node/N, node/N/edit, etc).

Other paths use our custom %project_node menu placeholder, so that we only add the tab on project nodes (e.g. node/N/cvs-access, node/%project_node/edit/issues, etc).

So, there's no easy/reliable way to get the project node from the menu context since you basically always have to call menu_get_object() twice, once using 'node' (the default), and once using 'project_node'. This is a problem, for example, for all the project-specific blocks that require a project context.

Therefore, we should just add a helper function for this, something like:

/**                                                                             
 * Get the project node context from the currently active menu, if any.         
 *                                                                              
 * @return                                                                      
 *   A fully loaded project $node object if the currently active menu has a     
 *   project node context, or NULL if the menu isn't pointing to a project.     
 */
function project_get_project_from_menu() {
  $node = menu_get_object('project_node');
  if (empty($node)) {
    $node = menu_get_object('node');
  }
  return (!empty($node) && $node->type == 'project_project') ? $node : NULL;
}

Any objections or thoughts?

Comments

dww’s picture

Or, this (suggested by chx) -- I considered cramming everything into 1 line like this at first but wanted to make it a tad more readable. But, I guess this is more clear:

/**                                                                             
 * Get the project node context from the currently active menu, if any.         
 *                                                                              
 * @return                                                                      
 *   A fully loaded project $node object if the currently active menu has a     
 *   project node context, or NULL if the menu isn't pointing to a project.     
 */
function project_get_project_from_menu() {
  if (($node = menu_get_object('project_node')) ||
      ($node = menu_get_object('node')) && $node->type == 'project_project') {
    return $node;
  }
}
dww’s picture

Ohh, to clarify, #1 is actually my code. chx proposed this:

function project_get_project_from_menu() {
  if ((($node = menu_get_object('project_node')) || ($node = menu_get_object('node'))) && $node->type == 'project_project') {
    return $node;
  }
}

But that's wasteful, since menu_get_object('project_node') already tests $node->type for us.

dww’s picture

Status: Active » Fixed

Tested and committed to HEAD, along with patches to change various spots in project and project_issue to use this.
http://drupal.org/cvs?commit=195948
http://drupal.org/cvs?commit=195950

dww’s picture

Issue tags: +needs drupal.org deployment

When this is deployed, we need to make sure project, project_issue, and cvslog are all updated at the same time, or we'll get undefined function errors.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

dww’s picture

Issue tags: -needs drupal.org deployment

This is now live on d.o. Danithica can fix pivots_block.module to take advantage of this...