public function License::preSave in Commerce License 8.2
Acts on an entity before the presave hook is invoked.
Used before the entity is saved and before invoking the presave hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed. This is different from its counterpart in the Field API, FieldItemListInterface::preSave(), which is fired on all field translations automatically. @todo Adjust existing implementations and the documentation according to https://www.drupal.org/node/2577609 to have a consistent API.
Parameters
\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.
Throws
\Exception When there is a problem that should prevent saving the entity.
Overrides ContentEntityBase::preSave
See also
\Drupal\Core\Field\FieldItemListInterface::preSave()
File
- src/
Entity/ License.php, line 84
Class
- License
- Defines the License entity.
Namespace
Drupal\commerce_license\EntityCode
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
// Act when the license state changes, or the license is new.
// (Note that $this->original is not set on new entities.)
if (isset($this->original) && $this
->getState()
->getId() != $this->original
->getState()
->getId() || !isset($this->original)) {
// If the state is being changed to 'active', set the granted and
// expiration timestamps, and notify the license type plugin. We act on
// preSave() rather than postSave() so that the license plugin can set
// values on the license. HOWEVER, this means that if something acts in
// hook_entity_presave() to prevent saving, by throwing an exception, the
// license entity will be unsaved, but the license plugin will have
// granted the license, leaving it in an incorrect state.
// TODO: override doPreSave() in LicenseStorage to catch exceptions and
// revert the grant if the save is cancelled.
if ($this
->getState()
->getId() == 'active') {
// The state is moved to 'active', or the license was created active:
// the license activates.
$this
->getTypePlugin()
->grantLicense($this);
// Set timestamps.
$activation_time = \Drupal::service('datetime.time')
->getRequestTime();
if (empty($this
->getGrantedTime())) {
// The license has not previously been granted, and is therefore being
// activated for the first time. Set the 'granted' timestamp.
$this
->setGrantedTime($activation_time);
}
else {
// The license has previously been granted, and is therefore being
// re-activated after a lapse. Set the 'renewed' timestamp.
$this
->setRenewedTime($activation_time);
}
// Set the expiry time on a new license, but allow licenses to be
// created with a set expiry, such as in the case of a migration.
if (!$this
->getExpiresTime()) {
$this
->setExpiresTime($this
->calculateExpirationTime($activation_time));
}
}
// The state is being moved away from 'active'.
if (isset($this->original) && $this->original
->getState()
->getId() == 'active') {
// The license is revoked.
$this
->getTypePlugin()
->revokeLicense($this);
}
}
}