View source
<?php
namespace Drupal\preview_link;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PreviewLinkStorage extends SqlContentEntityStorage implements PreviewLinkStorageInterface {
protected $time;
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityFieldManagerInterface $entity_field_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, UuidInterface $uuid, TimeInterface $time) {
parent::__construct($entity_type, $database, $entity_field_manager, $cache, $language_manager, $memory_cache, $entity_type_bundle_info, $entity_type_manager);
$this->uuidService = $uuid;
$this->time = $time;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('database'), $container
->get('entity_field.manager'), $container
->get('cache.entity'), $container
->get('language_manager'), $container
->get('entity.memory_cache'), $container
->get('entity_type.bundle.info'), $container
->get('entity_type.manager'), $container
->get('uuid'), $container
->get('datetime.time'));
}
public function getPreviewLinkForEntity(ContentEntityInterface $entity) {
return $this
->getPreviewLink($entity);
}
public function getPreviewLink(ContentEntityInterface $entity) {
$result = $this
->loadByProperties([
'entity_type_id' => $entity
->getEntityTypeId(),
'entity_id' => $entity
->id(),
]);
return $result ? array_pop($result) : FALSE;
}
public function createPreviewLinkForEntity(ContentEntityInterface $entity) {
return $this
->createPreviewLink($entity
->getEntityTypeId(), $entity
->id());
}
public function createPreviewLink($entity_type_id, $entity_id) {
$preview_link = $this
->create([
'entity_id' => $entity_id,
'entity_type_id' => $entity_type_id,
]);
$preview_link
->save();
return $preview_link;
}
public function create(array $values = []) {
return parent::create($values + [
'token' => $this
->generateUniqueToken(),
'generated_timestamp' => $this->time
->getRequestTime(),
]);
}
public function save(EntityInterface $entity) {
if ($entity
->regenerateToken()) {
$entity
->setToken($this
->generateUniqueToken());
}
return parent::save($entity);
}
protected function generateUniqueToken() {
return $this->uuidService
->generate();
}
}