Found an error in the function that removes and value from a multivalue entity reference field

PHP Fatal error: Cannot use object of type stdClass as array in /httpdocs/sites/all/modules/wrappers_custom/includes/node/AffiliateLeadNodeWrapper.php on line 145, referer: http://energiemarkt.discount/partner-backend/lead-management/lead/749/edit

This refferes the line
unset($existing_value[$i]);
which should be
unset($existing_values[$i]);

Old error

function removeFromCommissionRef($value) {
    if ($value instanceof WdNodeWrapper) {
      $value = $value->value();
    }
    $existing_values = $this->get('field_commission_ref');
    if (!empty($existing_values)) {
      foreach ($existing_values as $i => $existing_value) {
        if (!empty($existing_value) && entity_id('node', $existing_value) == entity_id('node', $value)) {
          unset($existing_value[$i]);
        }
      }
    }
    $this->set('field_commission_ref', array_values($existing_values));
    return $this;
  }

Working

function removeFromCommissionRef($value) {
    if ($value instanceof WdNodeWrapper) {
      $value = $value->value();
    }
    $existing_values = $this->get('field_commission_ref');
    if (!empty($existing_values)) {
      foreach ($existing_values as $i => $existing_value) {
        if (!empty($existing_value) && entity_id('node', $existing_value) == entity_id('node', $value)) {
          unset($existing_values[$i]);
        }
      }
    }
    $this->set('field_commission_ref', array_values($existing_values));
    return $this;
  }

Comments

fox_01 created an issue. See original summary.

zengenuity’s picture