2

I added a new field (houses) to user accounts. This field is a list of a collection fields.

I need to list all the fields in a user field collection (I can via the FieldCollectionItem::loadMultiple(array(12,45,...)) and remove all of them.

This is my code:

$array_of_fieldcollection = get_array_of_fieldcollection_of_user();

$fieldscollection = FieldCollectionItem::loadMultiple($array_of_fieldcollection);

foreach($fieldscollection as $fieldcollection){
  $fieldcollection->delete();
}

When I execute it and go to the user profile edit page, I get this error:

The website encountered an unexpected error. Please try again later.
More detail in the Apache log.

TypeError: Argument 1 passed to Drupal\Core\Entity\Entity\EntityFormDisplay::buildForm() must implement interface Drupal\Core\Entity\FieldableEntityInterface, null given, called in /var/www/www.orlengine.com/modules/contrib/old_field_collection/src/Plugin/Field/FieldWidget/FieldCollectionEmbedWidget.php on line 71 in drupaldirectory/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php on line 159, referer: https://192.168.1.45/user/9

Isn't my code the proper way to delete all the field of a field collection?

2 Answers 2

4
+50

The code you've shared is correct. The problem is that when we attach a Field Collection field to "User" entity the Host Entity of Field Collection is not set.

Because of which when we delete a Field Collection Entity programmatically the Host Entity field value (i.e. User in this case) is not updated with deletion. This causes Drupal to load Field collection which doesn't exists in system.

Rewriting the deletion code below:

use Drupal\field_collection\Entity\FieldCollectionItem;

// Load all field collections to delete.
$field_collections = FieldCollectionItem::loadMultiple(array(1, 2));
foreach($field_collections as $field_collection){
  // Delete field collection.
  $field_collection->delete();
}
4
  • but in the code source, the delete method of a fieldCollectionItem contains "$this->deleteHostEntityReference();". So, if I delete a FieldCollectionItem, the hos entity reference (here the user) must be delete no ? (file field_collection/src/Entity/FieldCollectionItem.php ) However, if I user the delete method of the FieldCollectionItem, I have the error I show in my fisrt post Sep 17, 2016 at 9:40
  • You're right, and that works fine with node, but not with user entity. Because when we attach field collection to user entity, and save user the host entity is not set properly of field collection, check my comment above.
    – Yogesh
    Sep 17, 2016 at 9:44
  • So, you confirm me the solution is to load the user entity and delete manually the field who contain the fieldcollection reference ? (no properly way ..) Sep 17, 2016 at 10:01
  • Yes, until we've a proper fix for host entity when using with user entity
    – Yogesh
    Sep 17, 2016 at 10:21
0

I encountered the same error when deleting field collection items on User entities. Below some code to unset the field manually on the User entity.

$id = 'Your FC field id';
$field = 'Your FC field name';

public function getFieldDelta($id, $field) {
  $fields = $this->get($field);

  foreach ($fields as $delta => $item) {
    if ($item->value == $id) {
      return $delta;
    }

  }
}

$delta = $this->getFieldDelta($id, $field);
unset($user->$field[$delta]);
$user->save();

The code sample above has a function getFieldDelta() that returns the delta of a field collection item if the provided item id matches one in the fields. With the delta the item in the user field is unset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.