0

How can I configure programmatically a node object to open in a new tab? I thought maybe in the $node->path array?

enter image description here

I don't see where and how I can achieve this... There is nothing such as a rendered <a> on which I can add a target="_blank".

EDIT (more specific situation):

I've got a content type "news". It is possible to create news in 2 different ways:

  • An editor user creates manually through the admin interface (basic).
  • A custom module imports data from external RSS feeds everytime a piece of news is published in it.

I've got a view that display the list of news (just title, truncated text, date, logo...). When you click on a news' title, you access the full node view (normal). If it is an internal news (created manually), it displays normally. If it is an imported news, however, I redirect using drupal_goto to the source of the news (that I saved on node creation).

I want to make this very redirection in a new tab. It's quite complicated because it is originally an internal link (node/nid) that redirects to an external link (http://www.sourcesite.com).

I can make the difference between an imported/not imported node in the hook_preprocess_views_view. But I can't assign it something like a target _blank or whatever it exists to make this node opened in a new tab.

10
  • Seems that there is a serial downvoter this morning... Please leave a comment and justify why I'm wrong.
    – Djouuuuh
    Commented Jul 9, 2014 at 9:51
  • 1
    Can you be more specific as to which links should open like that? Every link to a node anywhere on the site, including user-generated content? Just one particular page/view/block? Only nodes of a certain content type?
    – Andy
    Commented Jul 9, 2014 at 10:23
  • I tried to be more specific in my edit, @Andy
    – Djouuuuh
    Commented Jul 9, 2014 at 12:28
  • I have a solution in mind: is it ok/good if the logic gets used anywhere a news node in teaser form is shown on the site (ie it's not restricted to the one view)?
    – Andy
    Commented Jul 9, 2014 at 12:43
  • Yes, there are 2 places in the site where this view appears and I'd like to achieve that for both places, so I guess it's ok! Summary: everytime I've got this view with imported news, I want them to be opened in a new tab.
    – Djouuuuh
    Commented Jul 9, 2014 at 12:47

2 Answers 2

0

This is a theming question and there's a choice to be made about whether to use a Views or a node template. By and large when displaying entities in views, I tend to use a particular view mode, because then I can display them similarly outside of Views if necessary. To do that:

  1. Choose which view mode to use. In your case use the Teaser view mode, or if that's not an option create a new one.
  2. Ensure your view's configured to display that view mode. For more info, download and install advanced_help, and read from the Views help at least Row styles and its children Fields and Node.
  3. Check in the node template you're using: is there a variable that would let you add a target attribute to the title link? (There isn't in core node.tpl.php but there might be in your theme.) For more info you can read Overriding themable output and Core templates and suggestions. I'd suggest using devel_themer to see what template is actually used - make sure you've done step 2 first. The template should either be node.tpl.php or start with node-- and end .tpl.php.
  4. If there wasn't a variable in step 3, then modify the template to have one, eg. by adding $title_link_attributes.
  5. Implement hook_preprocess_node(). Check if the node is of the correct type and view mode and modify the variable appropriately. (And if you created a new variable, ensure that you set it to the empty string for other view modes and content types to avoid warnings.) For more info, see Setting up variables for use in a template (preprocess and process functions).

Let me know if you have any questions, or if that approach isn't appropriate for any reason.

Btw I gave a non-JS answer because I thought that was a requirement, but now I'm not so sure.

9
  • I'm going to test that solution first and I'll come back to you if I need it. Thank you anyway for your time!
    – Djouuuuh
    Commented Jul 9, 2014 at 13:39
  • Uhh... I can't access the information I need in the template part of the code. I guess I'm gonna explain the SEO guy that I have truly no choice but doing that in JS like I had. Well thanks a lot for trying to help! It's very kind of you.
    – Djouuuuh
    Commented Jul 9, 2014 at 13:52
  • @Djouuuuh You can't use the node object?
    – Andy
    Commented Jul 9, 2014 at 13:59
  • node object is empty (ofc) in views-view-list-pwe-news.tpl.php where I should add a variable or a class to target with my JS. I'm afraid I can't, indeed.
    – Djouuuuh
    Commented Jul 9, 2014 at 14:02
  • @Djouuuuh You're trying a different approach, using Views templates rather than node. It can be made to work, but as explained above I tend to do it differently.
    – Andy
    Commented Jul 9, 2014 at 14:10
0

I finally achieve what I wanted. I succeed to access the info (hidden very deeply) in my template.php file within the following hook:

function pwe_2014_preprocess_views_view_fields(&$vars) {
  if(isset($vars['view']) && $vars['view']->name == "pwe_news") {
    foreach($vars['view']->result as $key => $result) {
      if(isset($result->node_feeds_item_nid) && ! is_null($result->node_feeds_item_nid)) {
        $vars['view']->result[$key]->class = "external";
      } else {
        $vars['view']->result[$key]->class = "internal";
      }
    };
  }
}

I assigned that new class to a new div in my views-view-fields.tpl.php:

<div class="<?php echo $row->class ?>">
...
</div>

And target that class with JavaScript:

$('.view-pwe-news .views-row .external a').attr('target', '_blank');

Not easy to find the right theme function where I could access the right info.

1
  • In case you didn't try/know, devel themer can help you identify what template/theme function is responsible for particular markup, which can be very useful for nested templates like this.
    – Andy
    Commented Jul 14, 2014 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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