function paragraphs_field_update in Paragraphs 7
Implements hook_field_update().
Care about removed paragraph items.
File
- ./
paragraphs.module, line 750 - Paragraphs hooks and common functions.
Code
function paragraphs_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
// Prevent workbench moderation from deleting paragraphs on node_save() during
// workbench_moderation_store(), when $host_entity->revision == 0.
if (!empty($entity->workbench_moderation['updating_live_revision'])) {
return;
}
// Prevent State Flow Entity from deleting paragraphs on node_save()
// when adding new paragraphs item and a published revision of the node exist.
// @see state_flow_entity_exit().
if (!empty($entity->state_flow) && isset($entity->revision) && $entity->revision === FALSE) {
return;
}
$items_original = !empty($entity->original->{$field['field_name']}[$langcode]) ? $entity->original->{$field['field_name']}[$langcode] : array();
$original_by_id = array_flip(paragraphs_field_item_to_ids($items_original));
foreach ($items as $item) {
unset($original_by_id[$item['value']]);
}
// 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($entity->revision)) {
db_update('paragraphs_item')
->fields(array(
'archived' => 1,
))
->condition('item_id', $ids, 'IN')
->execute();
}
else {
// Delete unused paragraph items now.
foreach (paragraphs_item_load_multiple($ids) as $item) {
$item
->setHostEntity($entity_type, $entity, $langcode, FALSE);
$item
->deleteRevision(TRUE);
}
}
}
}