0

I'm using Drupal 7, and trying to work some magic from within a hook_node_presave() function.

I have a node reference field, and I'm trying to get the name of the referenced node rather than nid. When I trace out the field ($node->field_my_name) I get the following

Array
(
    [und] => Array
        (
            [0] => Array
                (
                    [nid] => 23
                )

        )

)

I wondering if there is a way to get the title without doing a full load of the referenced node. Is there anyway I can do this with a token? Would this no be an issue if I were using entity reference rather than references?

2
  • 1
    more than likely, the node is cached anyway and node_load(23) won't too adversely effect performance. Jan 13, 2012 at 18:08
  • good point, I'm just really surprised that there is no function to load the title of a node, give the nid, I just tried to do it with a static query, but for some reason it didn't work
    – user379468
    Jan 13, 2012 at 18:39

1 Answer 1

1

You could use entity_label() function.

The function takes the entire entity as an argument, and it should already be loaded.
I would just load the node. As the other author suggested, it is very likely to be already cached in a hook_node_presave() call.

0

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.