1

When you call menu_get_object(), the first argument defaults to 'node' if no arguments are passed to the function. The code uses the $type variable to check if the current object is of that type, using $router_item['load_functions'][$position] == $type . '_load'.

Sometimes, I just want to get the current menu object, regardless of it's type. In other words, when I'm looking at user/123, I want the user object, and when I'm viewing node/456, I want the node object.

Why does menu_get_object force a single entity type?

What is the best way to retrieve the current menu object, regardless of it's type?

2 Answers 2

3

As you can see in that function, it does nothing more than:

$router_item = menu_get_item($path);
return $router_item['map'][$position];

However, I'm not sure how that will help you, for pretty much all objects, you need to know what it is to work with it as there are just some attributes.

7
  • Yes, I have seen that. It seems a good solution, although I'm not 100% sure if it will get me in trouble somewhere down the road. I think it will work for me, because I want to get the value of a certain field, and Field API is supposed to work with all kinds of entities (even though field_get_items() requires a $type argument as well). Jun 23, 2011 at 9:44
  • marcvangend where's solution?
    – Nikit
    Jun 23, 2011 at 10:05
  • @marcvangend: Yes, always need $entity and $entity_type when working with entities, because there no general way to extract the type from $entity. (As opposed to $bundle and $id, which you can extract).
    – Berdir
    Jun 23, 2011 at 11:27
  • @Berdir: Thanks. At the risk of drifting off-topic... isn't that strange? I'd say that very entity load could just set an $entity->type property automatically. I see that you have already wondered about that at drupal.org/node/915058. Jun 23, 2011 at 12:44
  • @marcvanged: Yes, there should be something. But.. nodes for example already have a type attribute (which is the bundle, actually), adding that would have broken that. So it would have to be entity_type (which might already be used by other modules, for example userpoints transactions do..).. I think the real solution to this problem is using real classes for entities, not just stdClass, which will maybe happen in D8.
    – Berdir
    Jun 23, 2011 at 13:28
0
if (arg(0) == 'node') {
  $node = menu_get_object();
}
else if (arg(0) == 'user') {
  $user = menu_get_object('user');
}

:) or try this:

  $obj = menu_get_object(arg(0));

although that won't work in a url like this: http://URL/some_arg0/REQUIRED_OBJECT/some_arg2

2
  • I considered that too, but the first method is not generic enough for my use case. Regarding the second method, as you say, it doesn't seem right to assume that arg(0) is always equal to the entity type. Jun 23, 2011 at 9:36
  • You can use context.
    – Nikit
    Jun 23, 2011 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.