public function GeolocationGeometryBase::postSave in Geolocation Field 8.3
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/
geolocation_geometry/ src/ Plugin/ Field/ FieldType/ GeolocationGeometryBase.php, line 83
Class
- GeolocationGeometryBase
- Class Geolocation Geometry Base.
Namespace
Drupal\geolocation_geometry\Plugin\Field\FieldTypeCode
public function postSave($update) {
parent::postSave($update);
$entity = $this
->getEntity();
$entity_storage = \Drupal::entityTypeManager()
->getStorage($entity
->getEntityTypeId());
if (!is_a($entity_storage, '\\Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorage')) {
return FALSE;
}
/** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $entity_storage */
$table_mapping = $entity_storage
->getTableMapping();
$field_storage_definition = $this
->getFieldDefinition()
->getFieldStorageDefinition();
if ($entity
->getEntityType()
->isRevisionable()) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$query = \Drupal::database()
->update($table_mapping
->getDedicatedRevisionTableName($field_storage_definition));
if (!empty($this->values['geojson'])) {
$query
->expression($field_storage_definition
->getName() . '_geometry', 'ST_GeomFromGeoJSON(' . $field_storage_definition
->getName() . '_geojson)');
$query
->expression($field_storage_definition
->getName() . '_wkt', 'ST_AsText(ST_GeomFromGeoJSON(' . $field_storage_definition
->getName() . '_geojson))');
}
elseif (!empty($this->values['wkt'])) {
$query
->expression($field_storage_definition
->getName() . '_geometry', 'ST_GeomFromText(' . $field_storage_definition
->getName() . '_wkt, 4326)');
$query
->expression($field_storage_definition
->getName() . '_geojson', 'ST_AsGeoJSON(ST_GeomFromText(' . $field_storage_definition
->getName() . '_wkt, 4326))');
}
if (empty($this->values['data'])) {
$query
->fields([
$field_storage_definition
->getName() . '_data' => serialize(NULL),
]);
}
$query
->condition('entity_id', $entity
->id());
$query
->condition('revision_id', $entity
->getRevisionId());
$query
->condition('bundle', $entity
->bundle());
$query
->condition('delta', $this
->getName());
$query
->condition('langcode', $this
->getLangcode());
$query
->execute();
}
$query = \Drupal::database()
->update($table_mapping
->getDedicatedDataTableName($field_storage_definition));
if (!empty($this->values['geojson'])) {
$query
->expression($field_storage_definition
->getName() . '_geometry', 'ST_GeomFromGeoJSON(' . $field_storage_definition
->getName() . '_geojson)');
$query
->expression($field_storage_definition
->getName() . '_wkt', 'ST_AsText(ST_GeomFromGeoJSON(' . $field_storage_definition
->getName() . '_geojson))');
}
elseif (!empty($this->values['wkt'])) {
$query
->expression($field_storage_definition
->getName() . '_geometry', 'ST_GeomFromText(' . $field_storage_definition
->getName() . '_wkt, 4326)');
$query
->expression($field_storage_definition
->getName() . '_geojson', 'ST_AsGeoJSON(ST_GeomFromText(' . $field_storage_definition
->getName() . '_wkt, 4326))');
}
if (empty($this->values['data'])) {
$query
->fields([
$field_storage_definition
->getName() . '_data' => serialize(NULL),
]);
}
$query
->condition('entity_id', $entity
->id());
$query
->condition('bundle', $entity
->bundle());
$query
->condition('delta', $this
->getName());
$query
->condition('langcode', $this
->getLangcode());
$query
->execute();
return FALSE;
}