function farm_entity_entity_presave in farmOS 2.x
Implements hook_entity_presave().
Forces revisions on all farm entities if the entity type supports them and the bundle has them enabled. This removes the option for users to disable a revision per-entity but as JSON:API doesn't support revisions yet, this is a trade-off that allows us to create revisions consistently on both the UI and the API.
File
- modules/
core/ entity/ farm_entity.module, line 113 - Contains farm_entity.module.
Code
function farm_entity_entity_presave(EntityInterface $entity) {
// Only apply to farm controlled entities.
$entity_types = [
'asset',
'log',
'plan',
'quantity',
];
if (!in_array($entity
->getEntityTypeId(), $entity_types)) {
return;
}
// Force create new revision as json api doesn't do that by default.
// @see https://www.drupal.org/project/drupal/issues/2993557
// @see https://www.drupal.org/project/drupal/issues/2795279
// @see https://github.com/json-api/json-api/pull/824
if ($entity->type->entity
->shouldCreateNewRevision() && $entity
->getEntityType()
->isRevisionable()) {
/** @var \Drupal\Core\Entity\RevisionLogInterface $entity */
// Always create a new revision.
$entity
->setNewRevision(TRUE);
// If the new revision log message matches the original, then set a blank
// revision log message. We don't want the same message repeated across
// every revision created by the API.
if (!empty($entity->original)) {
if ($entity->original
->get('revision_log_message')->value == $entity
->get('revision_log_message')->value) {
$entity
->setRevisionLogMessage('');
}
}
// Set the user ID and creation time.
$entity
->setRevisionUserId(\Drupal::currentUser()
->getAccount()
->id());
$entity
->setRevisionCreationTime(\Drupal::time()
->getRequestTime());
}
}