When I run a migration that uses a field of this type, I get the following warning (an informational message as seen in "migrate" log) ...

Undefined property: stdClass::$nid File nodereference_count.module, line 165

The error is coming because the code doesn't check if the "nid" property is populated - which it isn't in the case of a node that is yet to be saved.

A simple solution is to change the following code ...

function nodereference_count_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if ($field['type'] == 'nodereference_count' && $entity_type == 'node') {
    $items[0]['count'] = nodereference_count_get_count($instance['settings'], $entity->nid);
  }
}

to this ...

function nodereference_count_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if ($field['type'] == 'nodereference_count' && $entity_type == 'node'  && !empty($entity->nid) {
    $items[0]['count'] = nodereference_count_get_count($instance['settings'], $entity->nid);
  }
}

Will be attaching patch ...

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Jitesh Doshi’s picture

Version: 7.x-1.x-dev » 7.x-1.0
FileSize
693 bytes

Patch attached ...

John Franklin’s picture

Chris Charlton’s picture

@Jitesh, your patch has a typo/bug:

+  if ($field['type'] == 'nodereference_count' && $entity_type == 'node' && !empty($entity->nid) {

should be...

+  if ($field['type'] == 'nodereference_count' && $entity_type == 'node' && !empty($entity->nid)) {
Jitesh Doshi’s picture

FileSize
694 bytes

@Chris Charlton, thanks for catching that. I have fixed the bug/typo and attached a new patch. Please use this one.

Chris Charlton’s picture

Status: Active » Needs review