GlobalReferenceManager.php in Content Synchronizer 8
File
src/Service/GlobalReferenceManager.php
View source
<?php
namespace Drupal\content_synchronizer\Service;
use Drupal\Core\Database\Database;
use Drupal\Core\Entity\Entity;
class GlobalReferenceManager {
const SERVICE_NAME = 'content_synchronizer.global_reference_manager';
const GID_TABLE_NAME = 'content_synchronizer_global_reference';
const FIELD_GID = 'gid';
const FIELD_ENTITY_ID = 'entity_id';
const FIELD_ENTITY_TYPE = 'entity_type';
const BUFFER_TABLE_NAME = 'content_synchronizer_reference_buffer';
const FIELD_PARENT_ENTITY_ID = 'entity_gid';
const FIELD_REFERENCED_ENTITY_ID = 'referenced_entity_gid';
const FIELD_FIELD_NAME = 'field_name';
const FIELD_ORDER = 'item_order';
public function getEntityGlobalId(Entity $entity) {
$query = \Drupal::database()
->select(self::GID_TABLE_NAME)
->fields(self::GID_TABLE_NAME, [
self::FIELD_GID,
])
->condition(self::FIELD_ENTITY_TYPE, $entity
->getEntityTypeId())
->condition(self::FIELD_ENTITY_ID, $entity
->id());
if ($result = $query
->execute()
->fetchField()) {
return $result;
}
return NULL;
}
public function createEntityGlobalId(Entity $entity) {
$gid = (int) (microtime(TRUE) * 100) . '.' . $entity
->getEntityTypeId() . '.' . $entity
->id();
$data = [
self::FIELD_GID => $gid,
self::FIELD_ENTITY_ID => $entity
->id(),
self::FIELD_ENTITY_TYPE => $entity
->getEntityTypeId(),
];
\Drupal::database()
->insert(self::GID_TABLE_NAME)
->fields(array_keys($data))
->values($data)
->execute();
return $gid;
}
public function getEntityByGid($gid) {
$query = \Drupal::database()
->select(self::GID_TABLE_NAME)
->fields(self::GID_TABLE_NAME, [
self::FIELD_ENTITY_TYPE,
self::FIELD_ENTITY_ID,
])
->condition(self::FIELD_GID, $gid);
if ($result = $query
->execute()
->fetchAssoc()) {
return \Drupal::entityTypeManager()
->getStorage($result[self::FIELD_ENTITY_TYPE])
->load($result[self::FIELD_ENTITY_ID]);
}
return NULL;
}
public function getEntityTypeFromGid($gid) {
list($time, $entityTypeId, $entityLocalId) = explode('.', $gid);
return $entityTypeId;
}
public function createGlobalEntityByImportingEntityAndGid(Entity $entity, $gid) {
$data = [
self::FIELD_GID => $gid,
self::FIELD_ENTITY_ID => $entity
->id(),
self::FIELD_ENTITY_TYPE => $entity
->getEntityTypeId(),
];
try {
\Drupal::database()
->insert(self::GID_TABLE_NAME)
->fields(array_keys($data))
->values($data)
->execute();
} catch (\Exception $e) {
}
}
public function onEntityDelete(Entity $entity) {
\Drupal::database()
->delete(self::GID_TABLE_NAME)
->condition(self::FIELD_ENTITY_ID, $entity
->id())
->condition(self::FIELD_ENTITY_TYPE, $entity
->getEntityTypeId())
->execute();
}
public function getExistingEntityByGidAndUuid($gid, $uuid) {
if ($existing = $this
->getEntityByGid($gid)) {
return $existing;
}
try {
$entityType = $this
->getEntityTypeFromGid($gid);
$query = \Drupal::entityQuery($entityType)
->condition('uuid', $uuid);
$result = $query
->execute();
if (!empty($result)) {
$entity = \Drupal::entityTypeManager()
->getStorage($entityType)
->load(reset($result));
$this
->createGlobalEntityByImportingEntityAndGid($entity, $gid);
return $entity;
}
} catch (\Exception $e) {
}
return NULL;
}
}