Hi Jaza,

First of all thanks for this wonderful module. Really helped theming.

Maybe a bug on tpl_field_vars_entityreference_fieldvalue (line 534)

on line 544, you determine the entity type based on $field_instance['entity_type'],
but this value returns the entity type of the current entity and not the referenced entity.

In my case, I referenced a profile2 entity type. And my current entity is a node.
line 554 will execute a node_load because the current entity is node (wrong at this point but ok)
line 555 is the actual problem because $node->title will return an error, because $node is null.

I also looked at the available variables, there is no way to find out what type of entity is the target node except for what you did for taxonomy. Maybe it is best to just let it remain as target_id, and not try to load the refenced entity at all (except for taxonomy of course)? Or at the very least check if $node is empty before attempting $node->title?

For quick reference below:

/**
 * Field value callback for 'entityreference' field type.
 */
function tpl_field_vars_entityreference_fieldvalue(&$vars, $field, $field_name, $varname, $field_instance, $options) {
  $valuekeys = array(
    'target_id',
  );
  
  tpl_field_vars_fieldvalue_buildarray($vars, $field, $varname, $valuekeys);
  if (empty($vars[$varname])) {
    return;
  }

  $entity_type = $field_instance['entity_type'];
  if (!empty($field_instance['settings']['behaviors']['taxonomy-index'])) {
    $entity_type = 'taxonomy_term';
  }
  
  foreach (array_keys($vars[$varname]) as $k) {
    $title = NULL;
    $url = NULL;

    if ($entity_type == 'node') {
      $node = node_load($vars[$varname][$k]);
      $title = check_plain($node->title);
      $url = url('node/' . $vars[$varname][$k]);
    }
    elseif ($entity_type == 'user') {
      $user = user_load($vars[$varname][$k]);
      $title = check_plain($user->name);
      $url = url('user/' . $vars[$varname][$k]);
    }
    elseif ($entity_type == 'taxonomy_term') {
      $term = taxonomy_term_load($vars[$varname][$k]);
      $title = check_plain($term->name);
      $url = url('taxonomy/term/' . $vars[$varname][$k]);
    }
    
    if (!empty($vars[$varname][$k]) && !empty($title) && !empty($url)) {
      $vars[$varname][$k] = array(
        'target_id' => $vars[$varname][$k],
        'title' => $title,
        'url' => $url,
      );
    }
  }
  
  tpl_field_vars_fieldvalue_singleorarray($vars, $varname);
}
CommentFileSizeAuthor
#2 tpl-field-vars-2341811.patch602 bytesJaza

Comments

  • Jaza committed 36f0713 on master, 7.x-1.x
    #2341811 by bryanmanalo: correctly determine entity type for entity...
  • Jaza committed 36f0713 on master, 7.x-1.x
    #2341811 by bryanmanalo: correctly determine entity type for entity...
Jaza’s picture

Title: entity reference not working properly » Correctly determine entity type for entity reference field values
Version: 7.x-1.20 » 7.x-1.x-dev
Status: Active » Fixed
StatusFileSize
new602 bytes

Thanks bryanmanalo - I've committed a fix for this, see attached patch (which is the commit without linebreak changes).

Status: Fixed » Closed (fixed)

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

bryanmanalo’s picture

Thanks. Just saw this now. Hehe