Skip to main content

How to remove an entity reference item from a multi-value field

Posted on March 4, 2021


Use this function ItemList::removeItem

if ($entity->hasField('field_items')) {
  $items = $entity->get('field_items');
  for ($i = 0; $i < $items->count(); $i++) {
    if ($items->get($i)->target_id == $target_id) {
      $items->removeItem($i);
      // Caution: decrement the counter as removeItem() also does a rekey().
      $i--;
    }
  }
  // Don't forget to save the entity.
  $entity->save();
}

Source: https://www.drupal.org/project/drupal/issues/2904621#comment-12283951