public function FieldCollection::preSave in Field collection 8.3
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldType/FieldCollection.php \Drupal\field_collection\Plugin\Field\FieldType\FieldCollection::preSave()
Care about removed field collection items.
Support saving field collection items in
$item['entity'];
. This may be used to seamlessly create field collection items during host-entity creation or to save changes to the host entity and its collections at once.
Overrides EntityReferenceItem::preSave
File
- src/
Plugin/ Field/ FieldType/ FieldCollection.php, line 183
Class
- FieldCollection
- Plugin implementation of the 'field_collection' field type.
Namespace
Drupal\field_collection\Plugin\Field\FieldTypeCode
public function preSave() {
if ($field_collection_item = $this
->getFieldCollectionItem()) {
$host = $this
->getEntity();
// Handle node cloning
if ($host
->isNew() && !$field_collection_item
->isNew()) {
// If the host entity is new but we have a field_collection that is not
// new, it means that its host is being cloned. Thus we need to clone
// the field collection entity as well.
$field_collection_item = $field_collection_item
->createDuplicate();
}
// TODO: Handle deleted items
/*
$field_name = $this->getFieldDefinition()->field_name;
$host_original = $host->original;
$items_original = !empty($host_original->$field_name) ? $host_original->$field_name : [];
$original_by_id = array_flip(field_collection_field_item_to_ids($items_original));
foreach ($items as &$item) {
*/
// TODO: Handle deleted items
/*
unset($original_by_id[$item['target_id']]);
}
// If there are removed items, care about deleting the item entities.
if ($original_by_id) {
$ids = array_flip($original_by_id);
// If we are creating a new revision, the old-items should be kept but get
// marked as archived now.
if (!empty($host_entity->revision)) {
db_update('field_collection_item')
->fields(['archived' => 1])
->condition('target_id', $ids, 'IN')
->execute();
}
else {
// Delete unused field collection items now.
foreach (FieldCollectionItem::loadMultiple($ids) as $un_item) {
$un_item->updateHostEntity($host_entity);
$un_item->deleteRevision(TRUE);
}
}
}
*/
$this->newHostRevision = $host
->isNewRevision();
// If the host entity is saved as new revision, do the same for the item.
if ($this->newHostRevision) {
$field_collection_item
->setNewRevision();
// TODO: Verify for D8, may not be necessary
/*
// Without this cache clear entity_revision_is_default will
// incorrectly return false here when creating a new published revision
if (!isset($cleared_host_entity_cache)) {
list($entity_id) = entity_extract_ids($host_entity_type, $host_entity);
entity_get_controller($host_entity_type)->resetCache([$entity_id]);
$cleared_host_entity_cache = true;
}
*/
if ($host
->isDefaultRevision()) {
$field_collection_item
->isDefaultRevision(TRUE);
//$entity->archived = FALSE;
}
}
if ($field_collection_item
->isNew()) {
$field_collection_item
->setHostEntity($this
->getEntity(), FALSE);
}
$field_collection_item
->save(TRUE);
$this->target_id = $field_collection_item
->id();
$this->revision_id = $field_collection_item
->getRevisionId();
}
}