4

I need to load node author uid to be used as part of a href link inside template.php, I managed to add a contextual link according to this post, and call it from node.tpl.php but I can't access node author uid to complete my link. I have a view with the following path: /userview/%/post, link inside node.tpl.php it appears, but does not work. I tried to do it by custom module, but I do not know how to load the 'node being viewed' in a custom module, that's why I'm trying to do it in template.php. Any help will be appreciated.

This is my code in template.php

function MYTHEME_node_view_alter(&$build) { 

         $node_author = user_load($node->uid);

          // Add your own custom link
          $build['links']['node']['#links']['user-post'] = array(
            'title' => t('User post'), 
            'href' => '/userview/' . $node_author . '/post', 
            'html' => TRUE, 
            'attributes' => array(
              'title' => 'User post',
            ),
          );


        }
5
  • nid from url in template.php? I don´t know how to do that.
    – pinueve
    Jul 29, 2015 at 5:42
  • if you are getting node nid, then $node = node_load(nid); $node->uid gives uid
    – Abin
    Jul 29, 2015 at 5:43
  • You can access uid as $uid in node.tpl.php. Please refer here. Hope this will help you.
    – Abin
    Jul 29, 2015 at 5:53
  • thanks, but I need to access it from template.php. my site is multilingual, so I have to translate text links. I can´t translate contextual links from node.tpl.php
    – pinueve
    Jul 29, 2015 at 6:00
  • I could not load $node, I'm sure there is a way to do it, all I get is "Notice: Undefined variable: node"
    – pinueve
    Jul 29, 2015 at 6:20

1 Answer 1

2

You have just a tiny mistake , use $build['#node']->uid instead $node->uid :)

function MYTHEME_node_view_alter(&$build) { 

         $node_author = user_load($build['#node']->uid);

          // Add your own custom link
          $build['links']['node']['#links']['user-post'] = array(
            'title' => t('User post'), 
            'href' => '/userview/' . $node_author . '/post', 
            'html' => TRUE, 
            'attributes' => array(
              'title' => 'User post',
            ),
          );


        }
3
  • thanks zhilevan, but I have an error -- "Recoverable fatal error: Object of class stdClass could not be converted to string in click_node_view_alter() (line 102 -". Line 102 is 'href' => '/userview/' . $node_author . '/post', how can I fix it?
    – pinueve
    Jul 29, 2015 at 11:26
  • 1
    @pinueve you're welcome because you should use uid instead user object try this (if you want user id) 'href' => '/userview/' . $node_author->uid . '/post', or this if you want username 'href' => '/userview/' . $node_author->name . '/post',
    – Yuseferi
    Jul 29, 2015 at 12:12
  • zhilevan, that did the trick, I upvoted your answer, thanks a lot.
    – pinueve
    Jul 29, 2015 at 12:18

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.