View source
<?php
namespace Drupal\serial;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Database\Database;
class SerialSQLStorage implements ContainerInjectionInterface, SerialStorageInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function createStorageNameFromField(FieldDefinitionInterface $fieldDefinition, FieldableEntityInterface $entity) {
return $this
->createStorageName($entity
->getEntityTypeId(), $entity
->bundle(), $fieldDefinition
->getName());
}
public function createStorageName($entityTypeId, $entityBundle, $fieldName) {
$tableName = 'serial_' . md5("{$entityTypeId}_{$entityBundle}_{$fieldName}");
return Database::getConnection()
->escapeTable($tableName);
}
public function generateValueFromName($storageName, $delete = TRUE) {
$connection = Database::getConnection();
$transaction = $connection
->startTransaction();
try {
$uniqid = uniqid('', TRUE);
$sid = $connection
->insert($storageName)
->fields([
'uniqid' => $uniqid,
])
->execute();
$sid = isset($sid) ? $sid : 0;
if ($delete && $sid && $sid % 10 == 0) {
$connection
->delete($storageName)
->condition('sid', $sid, '<')
->execute();
}
return $sid;
} catch (\Exception $e) {
$transaction
->rollback();
watchdog_exception('serial', $e);
throw $e;
}
}
public function generateValue(FieldDefinitionInterface $fieldDefinition, FieldableEntityInterface $entity, $delete = TRUE) {
$storageName = $this
->createStorageNameFromField($fieldDefinition, $entity);
return $this
->generateValueFromName($storageName, $delete);
}
public function getSchema() {
$schema = [
'fields' => [
'sid' => [
'type' => 'serial',
'not null' => TRUE,
'unsigned' => TRUE,
'description' => 'The atomic serial field.',
],
'uniqid' => [
'type' => 'varchar',
'length' => 23,
'default' => '',
'not null' => TRUE,
'description' => 'Unique temporary allocation Id.',
],
],
'primary key' => [
'sid',
],
'unique keys' => [
'uniqid' => [
'uniqid',
],
],
];
return $schema;
}
public function getAllFields() {
return \Drupal::getContainer()
->get('entity_field.manager')
->getFieldMapByFieldType(SerialStorageInterface::SERIAL_FIELD_TYPE);
}
public function createStorage(FieldDefinitionInterface $fieldDefinition, FieldableEntityInterface $entity) {
$storageName = $this
->createStorageNameFromField($fieldDefinition, $entity);
$this
->createStorageFromName($storageName);
}
public function createStorageFromName($storageName) {
$dbSchema = Database::getConnection()
->schema();
if (!$dbSchema
->tableExists($storageName)) {
$dbSchema
->createTable($storageName, $this
->getSchema());
}
}
public function dropStorage(FieldDefinitionInterface $fieldDefinition, FieldableEntityInterface $entity) {
$this
->dropStorageFromName($this
->createStorageNameFromField($fieldDefinition, $entity));
}
public function dropStorageFromName($storageName) {
$dbSchema = Database::getConnection()
->schema();
$dbSchema
->dropTable($storageName);
}
public function initOldEntries($entityTypeId, $entityBundle, $fieldName, $startValue) {
$storage = $this->entityTypeManager
->getStorage($entityTypeId);
$query = $storage
->getQuery();
$bundleKey = $storage
->getEntityType()
->getKey('bundle');
$query
->condition($bundleKey, $entityBundle);
$query
->accessCheck(FALSE);
$entityIds = $query
->execute();
$updated = 0;
$storageName = $this
->createStorageName($entityTypeId, $entityBundle, $fieldName);
foreach ($entityIds as $entityId) {
$entity = $storage
->loadUnchanged($entityId);
$serial = $this
->generateValueFromName($storageName);
$entity->{$fieldName}->value = $startValue + $serial - 1;
if ($entity
->save() === SAVED_UPDATED) {
++$updated;
}
}
return $updated;
}
}