public function UserConsentItem::postSave in General Data Protection Regulation 8.2
Same name and namespace in other branches
- 8 modules/gdpr_consent/src/Plugin/Field/FieldType/UserConsentItem.php \Drupal\gdpr_consent\Plugin\Field\FieldType\UserConsentItem::postSave()
- 3.0.x modules/gdpr_consent/src/Plugin/Field/FieldType/UserConsentItem.php \Drupal\gdpr_consent\Plugin\Field\FieldType\UserConsentItem::postSave()
Defines custom post-save behavior for field values.
This method is called during the process of saving an entity, just after values are written into storage. This is useful mostly when the business logic to be implemented always requires the entity identifier, even when storing a new entity. For instance, when implementing circular entity references, the referenced entity will be created on pre-save with a dummy value for the referring entity identifier, which will be updated with the actual one on post-save.
In the rare cases where item properties depend on the entity identifier, massaging logic will have to be implemented on post-save and returning TRUE will allow them to be rewritten to the storage with the updated values.
Parameters
bool $update: Specifies whether the entity is being updated or created.
Return value
bool Whether field items should be rewritten to the storage as a consequence of the logic implemented by the custom behavior.
Overrides FieldItemBase::postSave
File
- modules/
gdpr_consent/ src/ Plugin/ Field/ FieldType/ UserConsentItem.php, line 69
Class
- UserConsentItem
- Plugin implementation of the 'gdpr_user_consent' field type.
Namespace
Drupal\gdpr_consent\Plugin\Field\FieldTypeCode
public function postSave($update) {
$definition = $this
->getFieldDefinition();
/* @var \Drupal\gdpr_consent\ConsentUserResolver\ConsentUserResolverPluginManager $plugin_manager */
$plugin_manager = Drupal::service('plugin.manager.gdpr_consent_resolver');
$resolver = $plugin_manager
->getForEntityType($definition
->getTargetEntityTypeId(), $definition
->getTargetBundle());
$user = $resolver
->resolve($this
->getEntity());
if ($user != NULL) {
$this
->set('user_id', $user
->id());
}
$should_log = FALSE;
if (!$update) {
// Always log on a create.
$should_log = TRUE;
}
else {
$field_name = $this
->getFieldDefinition()
->getName();
$original_value = $this
->getEntity()->original->{$field_name}->agreed;
if ($original_value != $this->agreed) {
$should_log = TRUE;
}
}
if ($should_log) {
$msg = Message::create([
'template' => 'consent_agreement_accepted',
]);
$msg
->set('user', $this->user_id);
$msg
->set('user_accepted', $this->user_id_accepted);
$msg
->set('agreement', [
'target_id' => $this->target_id,
'target_revision_id' => $this->target_revision_id,
]);
$msg
->set('notes', $this->notes);
$msg
->set('agreed', $this->agreed);
$msg
->save();
}
if ($user != NULL) {
return TRUE;
}
}