It would be great if there was a way to directly render the url or the path of a node reference.

Right now you get the options 'title' 'title link' and 'full node' in the fields display interface.For theming purposes it comes in handy when you have just the url, for example creating a 'highlights' slideshow on the homepage with links to different nodes, but you don´t want to display the title, just a 'read more...' button.

Comments

nuez’s picture

In the end i solved the issue by creating a specific field template for my Reference field, called 'field--field_name.tpl.php'. I put the file in my theme folder, and edited the file: instead of rendering $item, i printed $item['#href'], being able to use that value directly into a hyperlink.

dalai’s picture

Hello Nuez, could you provide your code as example? Have the same issue to solve. Thanks!

nuez’s picture

1. first you copy the field template (field.tpl.php) file from you're modules\field\theme directory.
2. you change it into a theme suggestion. If your field name is 'field_link' your file should be named 'field--field_link.tpl.php'.
3. Edit the file. The file´s output is originally:

<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <?php if (!$label_hidden) : ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item) : ?>
      <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
    <?php endforeach; ?>
  </div>
</div>

4. You change it into:

  <?php foreach ($items as $delta => $item) : ?>
      <a class="link button" href="<?php print($item['#href']); ?>"><?php print t("read more"); ?> </a>
    <?php endforeach; ?>

5. You can see that instead of using the entire $item array, i just output the 'href' part of it. I got rid of all de html, because its a specific theme output, so i don´t want it. You can check for different values in the $item array using print_r or if you are using the devel module you can use the kpr function which gives you a nice and readable output. I also added the html to output the hyperlink, I don´t know if that´s the best practice in terms of preprocessing etc, but it works for me.

6. you put the theme template file in your theme directory. As of DRUPAL 7 you don´t need to put the 'parent files' anymore, just the file is enough. Reset your theme cache in order for it to be recognized.

henrrrik’s picture

Component: Miscellaneous » Code: node_reference
Category: support » feature
Status: Active » Needs review
StatusFileSize
new3.72 KB

I've created a patch that adds a "URL, as plain text" display formatter for both user and node references, just like the one link.module provides.

In the meantime, I've also created a simple add-on module that provides this for node references for (http://drupal.org/sandbox/henrrrik/1094596) in case anyone else needs this functionality right away.

Jason Dean’s picture

henrrrik - You just saved me from a massive headache. The module works a treat - thanks! :)

Fidelix’s picture

I believe this is worth to be in the references module core, not in an addon module.

bsztreha’s picture

StatusFileSize
new4.85 KB

Hi,

i have another patch for this solution, with settings page, where you can more options for the generated plain url.

Two formatters:
Node ID: return the referenced nid

URL as plain text: return the URL (settings: Alias TRUE/FALSE, Absolute TRUE/FALSE)

bsztreha’s picture

StatusFileSize
new4.83 KB

Sorry, new patch without dpm :)

yched’s picture

Status: Needs review » Fixed
StatusFileSize
new14.04 KB

I made a few edits, and added similar formatters on the user_ref side.

Committed the attached patch.
Thanks !

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

wxman’s picture

Removed by me. Created a new issue.