0

I know how to get the nid for a node, but not for a teaser. I want to show a button in the teaser, created by a field with the PHP filter, that allows printing the node to pdf, but that requires the nid. The functions that work in the node, like menu_get_object(), don't work in the teaser.

Googling this has only brought up methods of printing a teaser if you already know the nid, not getting the nid if you only have the teaser. It should be available; it appears in the id for the teaser.

EDITED: I went with the suggestion posted below by f0ns. I don't understand why the nid is not available when it's in the teaser's id in the html, but that seems to be the case.

  • I'm not sure this is possible, if you're not not on the node page, there's really no context from which to retrieve a teaser's node. This may need to be done at the template level, or in a preprocess function for the teaser view. – thumbson Feb 11 '15 at 21:19
4

Use hook_node_view and for $view_mode == teaser you can add your logic.

https://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_view/7

| improve this answer | |
  • Thanks, looks like this would work in a module. My use case is a field on the teaser with the PHP filter. I've edited the question for clarity. – jn2 Feb 11 '15 at 20:53
  • I don't recommend using the php filter, why not to use it: drupal.stackexchange.com/questions/2509/… – Fons Vandamme Feb 11 '15 at 20:55
  • I agree with you, but I took over a site not developed with Drupal best practices (hacked core - dead kittens), and there's a lot of this in the site already. – jn2 Feb 11 '15 at 20:58
  • Still the method I suggested would be the fastest most secure way that I know of to implement the functionality that you want. – Fons Vandamme Feb 11 '15 at 21:01
  • Thanks, f0ns, your post led me in the right direction. This comment on the page you linked: api.drupal.org/comment/48278#comment-48278 gave me what I needed to get it done. I've marked yours the answer. (Note: for anyone who copies/pastes, there should be quotes around teaser as in $view_mode == 'teaser') – jn2 Feb 12 '15 at 19:44

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.