2

When I am using Drupal through the standard user interface, and click on a node title, Drupal will show the content in the main content area and (depending upon permissions and configuration) will add some tabs between the title and the content. If comments are enabled for the node, a comment entry form will appear below the content field. The tabs and the comment form can be seen in the image below, with the text in red coming from the node fields that resides in the database.

I want to do the same programmatically from the inside of a module. I.e. I want to pull a node from the database, and then display it so that it looks the same as if I've clicked on its title in the user interface.

Here is a more detailed description of what I want to do: I am writing a module to display a node for the user. This module have a button ("Get info") that the user may click. When the user clicks the button, it module pulls various data from a number of external sources, does some processing of this data, and stores the results as a new node object in the database (all this already works).

However, I want to show this node object to the user that pushed the button as soon as it has been created (i.e. to avoid having the user click through a link in order to view it). The user experience I am after his that the user clicks on a "Get info"-button, and then automatically is shown the new node that has been created programatically as a result of this click.

The last bit (showing the user the newly created node) does not work. I am asking for help to get that part of this module right.

I want the red text to be placed on the page by my module.

So far I've tried to use the following code (suggested here, but later deleted):

$node = node_load($nid);
$elements = node_view($node, 'full');
$full = drupal_render($elements);

This results in the follwing display on the page:

result of trying first suggestion

Note that there are no tabs ("View", "Edit", etc.) between the title and the content, and no comment form below. The blue "The title" is a hyperlink, i.e. it neccessary for a user to click through that hyperlink to get to a full node view with a comment form.

  • Pull it how? Display it where? What have you tried? – beth Dec 20 '12 at 23:53
  • It's hard to answer this question because I don't have much background about what larger goal you're trying to accomplish and how you are doing it. Are you putting this code in a module or a template? What do you want this module to do? – beth Jan 15 '13 at 6:10
  • Nearly all of the hooks and functions you could use to display a node will get the saved information from the database. Why don't you want to do it this way? – beth Jan 15 '13 at 15:32
1

If its the path name you want different how about simply using Pathauto

If not - for setting the page title u should do a drupal_set_message() with $node->title and disable it in the node object.

I hope this is of some help.

EDIT Trying to be of help again

'' However, I want to show this node object to the user that pushed the button as soon as it has been created (i.e. to avoid having the user click through a link in order to view it) ''

If it is one node at a time, since you are creating the node programmatically, how about redirecting the user to the node right after you create/save the new node?

0

Redirecting (suggested by SGosh) was the solution. The following single line of code does exactly what I want. After creating the node programmatically, I pull the $nid from the node object. Then:

drupal_goto('node/' . $nid);

will display it as a regular node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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