sparql_entity_storage-8.x-1.0-alpha8/src/SparqlEntityStorageEntityIdPluginBase.php
src/SparqlEntityStorageEntityIdPluginBase.php
<?php
declare(strict_types=1);
namespace Drupal\sparql_entity_storage;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a base plugin for entity ID generator plugins.
*/
abstract class SparqlEntityStorageEntityIdPluginBase extends PluginBase implements SparqlEntityStorageEntityIdPluginInterface, ContainerFactoryPluginInterface {
/**
* The entity for which the ID is being generated.
*/
protected ContentEntityInterface $entity;
/**
* The entity type manager service.
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* Constructs an entity ID generator plugin.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function setEntity(ContentEntityInterface $entity) {
$class = get_class($this->entityTypeManager->getStorage($entity->getEntityTypeId()));
if ($class != SparqlEntityStorage::class && !is_subclass_of($class, SparqlEntityStorage::class)) {
throw new \InvalidArgumentException("Passed entity must have a SPARQL storage.");
}
$this->entity = $entity;
return $this;
}
/**
* {@inheritdoc}
*/
public function getEntity() {
return $this->entity;
}
}
