0

I created a content type called successful story.

In user profile page user-profile.tpl.php, I have inserted a link to create a successful story

<?php print l(t('Add a successful story'), "node/add/successful-story"); ?>

For example, an user called user1 clicks on the link, and creates a successful story. I want to display the successful story the user created into their respective profile page

I add this code into template.php

<?php

function mytheme_preprocess_page(&$variables, $hook) {
  global $user;
  $uid = $user->uid;
  $nid = db_query("SELECT nid FROM {node} WHERE uid = :uid AND type = :type", array(":uid" => $uid, ":type" => 'successful story'))->fetchCol();
  $variables['ss_node'] = drupal_render(node_view(node_load($nid)));
}

?>

Then I add this code into user-profile.tpl.php

<?php

  global $user;
  $user_id = arg(1);

print $ss_node;

?>

I hope I'm clear. Thanks for your help !

0

It won't be available directly in the template. First you should query the content created by that user and then pass it to template file using preprocess function. Finally render the node.

First to find the node created by the user

$nid = db_query("SELECT nid FROM {node} WHERE uid = :uid AND type = :type", array(":uid" => $uid, ":type" => 'successful_story'))->fetchCol();

(Hope the user creates only one node or else you got to check it)

Then get the node content

$variables['ss_node'] = drupal_render(node_view(node_load($nid)));

The final code should be like

function template_preprocess_page(&$variables, $hook) {
  global $user;
  $uid = $user->uid;
  $nid = db_query("SELECT nid FROM {node} WHERE uid = :uid AND type = :type", array(":uid" => $uid, ":type" => 'successful_story'))->fetchCol();
  $variables['ss_node'] = drupal_render(node_view(node_load($nid)));
}

Finally you can just print the $ss_node in user-profile.tpl.php

  • I try this, I'll keep you in touch ! – Corentin Branquet Jan 5 '16 at 9:16
  • I got this error : Cannot redeclare template_preprocess_page(). I added the function code into template.php – Corentin Branquet Jan 5 '16 at 9:41
  • You shouldn't redeclare template_preprocess_page(), instead add the code inside the already existing template_preprocess_page(). – Suresh R Jan 5 '16 at 9:43
  • I added your code into the function in theme.inc file. But I get a bunch of error message. How can I give you the code I wrote ? – Corentin Branquet Jan 5 '16 at 9:53
  • I think you are doing it wrong! You should actually put the code in template.php, replace template_preprocess_page with theme_name_preprocess_page(). Say your theme name is 'mytheme', then it should be mytheme_preprocess_page() – Suresh R Jan 5 '16 at 9:59

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.