I'm not finding this is the docs...

I want to have my module declare a menu item that points to an existing node or view (this is a hack - see below for explainer), but I can't figure out what to use for the callback argument. For example:

<?php
  $items['admin/content/my-dynamic-content-admin-screen'] = array(
    'page callback' => '??????',  // what goes here to display a node or view?
    'page arguments' => ,
    'title' => 'My Custom admin menu',
  );
?>

Explainer:

The use-case here is that I want to be able to add links to the menu generated by the admin_menu module, which doesn't pick up user-generated links (it does pick up module-generated menu items, which is why I want to do the above - I want to be able to add links to items generated by other modules - mostly views).

Flame-avoidance notes:
* I'm concerned this may be a violation of best practices in that it would create unnecessary entries in the menu_router table, but I need the functionality and am willing to compromise my purity. If there's a better way to get what I need (short of patching admin_menu, which is not a realistic option at this time) please advise.

* I'm posting here (vs admin_menu) because I believe this is a general question regarding hook_menu; it's only incidentally related to admin_menu.

Comments

jaskho’s picture

I'm thinking I should go to the views module resources to find how to callback a view, but would still really like insight into what function to callback to in order to display a node.

Thanks!

mattyoung’s picture

>but would still really like insight into what function to callback to in order to display a node.

  $items['admin/content/my-dynamic-content-admin-screen'] = array(
    'page callback' => '_mymodule_show_node',  // what goes here to display a node or view?
    'title' => 'My Custom admin menu',
  );

function _mymodule_show_node() {
  $nid = 123;         // set it to the node id you want to display
  return node_page_view(node_load($nid));
}
jaskho’s picture

Thanks mattyoung ... node_page_view() is just the thing!

At first I didn't see the benefit in wrapping the call to node_page_view() in a module function, preferring to 'keep it simple' with, eg.,

<?php
  $items['admin/content/my-dynamic-content-admin-screen'] = array(
    'page callback' => 'node_page_view',
    'page arguments' => array (node_load(123)),
    'title' => 'My Custom admin menu',
  );
?>

But I'm thinking the point is to avoid hard coding the nid in the routing table. Although here it's hardcoded in the module, at least one doesn't have to rebuild the routing table to make a change. And ideally, one would add logic to determine the correct nid programmatically, perhaps from a config setting or some other source. You are truly committed to all things good and pure, I see!

mattyoung’s picture

This doesn't work:

'page arguments' => array (node_load(123))

The menu system can only store literal string values or path positional index as arguments. It cannot store node object. For this, you need to use the %node wildcard. Here is how you can do this:

  $items['admin/content/my-dynamic-content-admin-screen/%node'] = array(
    'page callback' => 'node_page_view',
    'page arguments' => array (3),
    'title' => 'My Custom admin menu',
  );

function node_to_arg() {
  return 12345;                // return the nid to display
}
jaskho’s picture

Somehow, it is working! I tested it before my post. Maybe it's saving the serialized result of the function call? (Or, more likely, I'm missing something). Interesting, though...

jaskho’s picture

Yes, it's saving a serialized instance of {node}, so it looks like it works... until you edit the underlying node. Thanks for saving me from potential heartache!

EvaldsUrtans’s picture

Thank you!!

WorldFallz’s picture

this is a very old post-- there's an admin_menu api that can add and alter the menu items now.