function paragraphs_field_presave in Paragraphs 7
Implements hook_field_presave().
Support saving paragraph items in
$item['entity'];
. This may be used to seamlessly create paragraph items during host-entity creation or to save changes to the host entity and its collections at once.
File
- ./
paragraphs.module, line 690 - Paragraphs hooks and common functions.
Code
function paragraphs_field_presave($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) {
$top_host = $host_entity;
while (method_exists($top_host, 'hostEntity')) {
$top_host = $top_host
->hostEntity();
}
// Prevent workbench moderation from deleting paragraphs on node_save()
// during workbench_moderation_store(), when $host_entity->revision == 0.
if (!empty($top_host->workbench_moderation['updating_live_revision'])) {
return;
}
foreach ($items as $key => &$item) {
// In case the entity has been changed / created, save it and set the id.
// If the host entity creates a new revision, save new item-revisions as
// well.
$entity = FALSE;
if (isset($item['entity'])) {
$entity = paragraphs_field_get_entity($item);
}
elseif (isset($item['revision_id'])) {
$entity = paragraphs_item_revision_load($item['revision_id']);
}
if ($entity) {
$entity
->setHostEntity($host_entity_type, $host_entity, $langcode, FALSE);
// If the host entity supports revisions and is saved as new revision, do
// the same for the item.
if (!empty($host_entity->revision)) {
$entity->revision = TRUE;
$is_default = entity_revision_is_default($host_entity_type, $host_entity);
// If an entity type does not support saving non-default entities,
// assume it will be saved as default.
if (!isset($is_default) || $is_default) {
$entity->default_revision = TRUE;
$entity->archived = FALSE;
}
}
if (isset($entity->removed) && $entity->removed) {
unset($items[$key]);
}
else {
$entity
->save(TRUE);
$item = array(
'value' => $entity->item_id,
'revision_id' => $entity->revision_id,
);
}
}
}
}