I did search Drupal web site for this, but after a hour, unable to sort this out.

Given a Drupal path (or alias, but I can use drupal_lookup_path() to go between them), how can I get the title of that path?

For example, given node/99 or forum/47, is there a way to get the title?
Probably menu_get_item()?
menu_get_item(NULL, 'node/99') returned empty array, so not sure if there is some other issue in the way I am calling it, or if this is not the way to do it.
Any pointers appreciated.

Comments

Chill35’s picture

drupal_get_title...? http://api.drupal.org/api/5/function/drupal_get_title

That will give you something like 'Content' on the front page... and the taxonomy term on term pages.
And the title of the node on a node/x page.

Caroline
A coder's guide to file download in Drupal
Who am I | Where are we
11 heavens

dman’s picture

Mmm. That's get the current page title.

I think the request here is to find the title of the target of other referenced links, given just their path - such as by making a filter that annotates any hrefs with the title="" alt value as found by introspecting the system.
Hm, doesn't this already exist?
Good question, sorry I can't think of a quick answer OTTOMH.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Chill35’s picture

yeah... right

hey dman how ur doing by the way

(funny, whenever I imagine you, I see you with a shaker on the other side of a bar, I wonder if it's something I read somewhere...)

Lost Caroline
Who am I | Where are we
11 heavens

Chill35’s picture

OTTOMH = ?

Out of the tip of my hand ?

vm’s picture

OTTOMH = Off The Top Of My Head

Chill35’s picture

No, but dman is a magician, doh.

Caroline
Who am I | Where are we
11 heavens

dman’s picture

Chill35’s picture

Don't mean to scare anyone. I am not that much of a clairvoyant.

I've been on your site a while ago, that's where I got the visual from, I think.

I hadn't seen that, though. Very Kewl dman...

Caroline
Who am I | Where are we
11 heavens

ksnyder’s picture

I needed this too. Try this:

<?php 
	global $nodeMenu;

	$nodeMenu = menu_get_item(NULL, "node/" . $node->nid, TRUE);
	
	print $nodeMenu['title'];
?>

This could probably use some code suggestions/cleanup from you more experienced PHP developers.

Chill35’s picture

Yeah I think the way to do it is to use menu_get_item, and I even think that you're calling it correctly.
Have you tried with 'TRUE'....?

menu_get_item(NULL, "node/43", TRUE);

Does that return an empty array?

Caroline
Who am I | Where are we
11 heavens

bwooster47’s picture

I think I am beginning to understand that what I need is not that easy - impossible, perhaps!

The issue is that I'm looking for a way to get title of what would be displayed given a Drupal path - which can be a menu item, a forum, a comment, for example:

node/281
node/281#comment-31
forum/11
taxonomy/term/18/9

The menu system handles this using callbacks at different levels of the menu - therefore, the only way to get this may be to execute the menu traversal that Drupal index.php performs.
Or, use HTTP to connect to the server and do a GET on the URL above !!!
Add to this, the menu path such as taxonomy/term/18/9 is clearly not a node - though it does have a title that can be used to display that page.

So, starting to understand why this seemingly simple request is not so simple.
No solution yet... (other than HTTP GET and look at title returned!)

criznach’s picture

Look at the function "menu_execute_active_handler" from menu.inc - pasted below...

The first part finds the menu callback that matches the current location. From that, you can find the default title for the callback. If the callback changes the default title, you'd have to execute the callback to get the correct title. Seems like a good thing to cache and update after edits rather than doing at display time...

function menu_execute_active_handler() {
  if (_menu_site_is_offline()) {
    return MENU_SITE_OFFLINE;
  }

  $menu = menu_get_menu();

  // Determine the menu item containing the callback.
  $path = $_GET['q'];
  while ($path && !isset($menu['callbacks'][$path])) {
    $path = substr($path, 0, strrpos($path, '/'));
  }

  if (!isset($menu['callbacks'][$path])) {
    return MENU_NOT_FOUND;
  }

  if (!function_exists($menu['callbacks'][$path]['callback'])) {
    return MENU_NOT_FOUND;
  }

  if (!_menu_item_is_accessible(menu_get_active_item())) {
    return MENU_ACCESS_DENIED;
  }

  // We found one, and are allowed to execute it.
  $arguments = isset($menu['callbacks'][$path]['callback arguments']) ? $menu['callbacks'][$path]['callback arguments'] : array();
  $arg = substr($_GET['q'], strlen($path) + 1);
  if (strlen($arg)) {
    $arguments = array_merge($arguments, explode('/', $arg));
  }

  return call_user_func_array($menu['callbacks'][$path]['callback'], $arguments);
}

http://www.trailheadinteractive.com
--- Featured Projects ---
http://www.montanakitesports.com
http://www.cmrussell.org
http://www.tdandh.com
http://www.cccsmt.org
http://www.universalsemensales.com

develcuy’s picture

Following works with Primary Links:

    $menu = menu_get_menu();
    $curi = urldecode(request_uri());
    if (isset($menu['path index'][$curi])) {
      $cmi = $menu['path index'][$curi];
      echo '<pre>'. print_r($menu['items'][$cmi]) .'</pre>';
   }

--
(3 John 1:2) Dear friend, I pray that you may enjoy good health and that all may go well with you, even as your soul is getting along well.

bwooster47’s picture

See How to get node object from a path? for another possible solution - will work for Drupal node paths only though, not for any other Drupal paths (like term/..., taxonomy/...)

Summary of the Drupal node lookup-solution:
Do a drupal_get_normal_path and then a node_load to get node object, which as all the node data, including title.

Or, use SQL:

    $node = db_fetch_object(db_query(&#039;SELECT n.nid, n.vid, n.type, r.title FROM {node} n INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE n.nid = %d&#039;, $nid));
benmirkhah’s picture

$path = drupal_get_normal_path('my/fancy/path/node');
$node = menu_get_object('node', 1, $path);
print $node->title;

jaypan’s picture

That won't work in Drupal 4.7 (which is the version this thread refers to).

Contact me to contract me for D7 -> D10/11 migrations.

jienckebd’s picture

For anybody still trying to do this for Drupal 7, here's a quick solution:

$item = menu_get_item('path/to/menu/item');
print $item['title'];

Documentation on menu_get_item() is at http://api.drupal.org/api/drupal/includes%21menu.inc/function/menu_get_i...

kla2t’s picture

The above D7 quick solution works fine, but not with aliased paths.
To make it cope with aliases, you have to check for the system path first:

$path = 'path/to/menu/item';
$normal_path = drupal_get_normal_path($path);
$item = menu_get_item($normal_path);
print $item['title'];
supradhan’s picture

It is working fine in normal node but it is giving blank title in view page. Any suggestions.

Some of the world's greatest feats were accomplished by people not smart enough to know they were impossible...- Doug Larson

dman’s picture

Depending on the type of page, the $item will be a different type of object. nodes (and probably panels) have 'title' but I think a view (and maybe a user or a term) would have a 'name' instead. It will vary.

What you should do is
* get and enable devel.module
* enter dpm($item); in the code at that point to see what the structure returned looks like.

Gik000’s picture

That gave me results on nodes and views

<?php
$normal_path = drupal_get_normal_path($path);
      $item = menu_get_item($normal_path);
      if(isset($item['title']) && $item['title']!=''){
        $title = $item['title'];
      } else {
        $title = $item['tab_root'];
      }
?>

hope someone will find it useful