The theme_hook_suggestions used for referenced node which should be displayed in a specific view mode (e.g. teaser or full) were using the wrong seperator ('-' instead of '__'). In this way it was not possible to overwrite the template for node for reference node view modes only.

Furthermore the comments of the function node_reference_preprocess_node were outdated after the change for #977322: noderef formatter: select which view mode to use for referenced nodes.

Comments

DjebbZ’s picture

Status: Needs review » Needs work

I confirm that the actual dev doesn't work, and that the patch solves the problem. I referenced a node from another, with the formatter "Rendered node", and was able to create a 'node--reference.tpl.php' file in my theme that worked as intented.

Marking the issue as "needs work", as it needs testing now.

DjebbZ’s picture

StatusFileSize
new1.74 KB

Just corrected suggestions name to correspond to Drupal convientions (1 hyphen to separate words, 2 to separate suggestions groups.)

DjebbZ’s picture

StatusFileSize
new1.25 KB

Basic instructions for the test.

yched’s picture

Status: Needs work » Closed (fixed)

Committed #2. Thanks !

Jeff Burnz’s picture

Status: Closed (fixed) » Active

Hi, I was working on this for someone trying to debug why the suggestions were not kicking in, I could only get it to work by declaring the preprocess function in hook_theme, I think this is a bug in D7 #939462: Specific preprocess functions for theme hook suggestions are not invoked ?

function node_reference_theme() {
  return array(
    'node_reference' => array (
      'variables' => array(),
    ),
  );
}
betarobot’s picture

That was me Jeff was helping to debug :) Maybe it's just a temporary fix, but can confirm #5 worked for me.

alan d.’s picture

In regards to #5: Yep, this is a core requirement. From the poll module that implements one of these theme suggestions.


/**
 * Implements hook_theme().
 */
function poll_theme() {
  $theme_hooks = array(
    'poll_results' => array(
      'template' => 'poll-results',
      'variables' => array('raw_title' => NULL, 'results' => NULL, 'votes' => NULL, 'raw_links' => NULL, 'block' => NULL, 'nid' => NULL, 'vote' => NULL),
    ),
  );
  // The theme system automatically discovers the theme's functions and
  // templates that implement more targeted "suggestions" of generic theme
  // hooks. But suggestions implemented by a module must be explicitly
  // registered.
  $theme_hooks += array(
    'poll_results__block' => array(
      'template' => 'poll-results--block',
      'variables' => $theme_hooks['poll_results']['variables'],
    ),
  );
  return $theme_hooks;
}

/**
 * Preprocess the poll_results theme hook.
 *
 * @see poll-results.tpl.php
 * @see poll-results--block.tpl.php
 */
function template_preprocess_poll_results(&$variables) {
  ...
  if ($variables['block']) {
    $variables['theme_hook_suggestions'][] = 'poll_results__block';
  }
}

A bit of a wtf moment working this out for the first time!

tommychris’s picture

#5 works for me, please add to git!

stevector’s picture

Status: Active » Needs review
StatusFileSize
new977 bytes

The $field variable in node_reference_preprocess_node() is not an array, just a string.

node_reference_field_formatter_view() only adds the the field_name to the node object.

// Line 454
 $nodes_display[$nid]->referencing_field = $field['field_name'];

The result is that only the first letter in the field name is added. So the theme_hook_suggestions array looks like this

array (
  0 => 'node__blog',
  1 => 'node__16',
  2 => 'node_reference',
  3 => 'node_reference__f',
  4 => 'node_reference__blog',
  5 => 'node_reference__f__blog',
)

instead of this:

array (
  0 => 'node__blog',
  1 => 'node__16',
  2 => 'node_reference',
  3 => 'node_reference__field_ref_featuredblog',
  4 => 'node_reference__blog',
  5 => 'node_reference__field_ref_featuredblog__blog',
)

The fix is relatively small.

stevector’s picture

Status: Needs review » Needs work

Re-reading this, I didn't really address the issue brought up in #5. Would it make more sense to start all of these with node__node_reference instead of node_reference?

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new1.1 KB

This one does away with the node_reference suggestion, instead reusing the node theme hook, and offering this as a suggestion to compliment that.

So your file names are:

node--node-reference.tpl.php
node--node-reference-field-foo.tpl.php
node--node-reference-page.tpl.php
node--node-reference-field-foo-page.tpl.php

So doesn't need hook_theme implementation.

apemantus’s picture

I can confirm that the patch in #11 works. I've just spent an annoying hour or two cursing devel_themer and not understanding why node-reference.tpl.php wasn't being picked up. With this patch, node--node-reference.tpl.php does get picked up.

j0rd’s picture

I've just ran into the bug, which patch #11 seems to fix. This should probably get committed and a new version made from it.

j0rd’s picture

Status: Needs review » Reviewed & tested by the community

Marking this as reviewed. We've got two patched confirmations, that it does what it's supposed to.

yched’s picture

Status: Reviewed & tested by the community » Fixed
StatusFileSize
new1.11 KB

Sorry for letting this drop off my radar.

I think the new template names in @larowlan's proposal in #11 make more sense. This feature never really worked, so I guess we can change the names :-/.

We need to use double '_' to separate the name parts, though.

Committed the attached patch.

Status: Fixed » Closed (fixed)

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