Synonym.php in Synonyms 2.0.x
File
src/Entity/Synonym.php
View source
<?php
namespace Drupal\synonyms\Entity;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\synonyms\SynonymInterface;
use Drupal\synonyms\ProviderPluginCollection;
class Synonym extends ConfigEntityBase implements SynonymInterface {
protected $provider_plugin;
protected $base_provider_plugin;
protected $provider_configuration = [];
protected $pluginCollection;
public function label() {
return $this
->getProviderPluginInstance()
->getPluginDefinition()['label'];
}
public function getProviderPluginInstance() {
return $this
->getPluginCollection()
->get($this
->getProviderPlugin());
}
public function getProviderPlugin() {
return $this->provider_plugin;
}
public function setProviderPlugin($plugin) {
$this->provider_plugin = $plugin;
}
public function getProviderConfiguration() {
$plugin = $this
->getProviderPluginInstance();
if ($plugin instanceof ConfigurableInterface) {
return $plugin
->getConfiguration();
}
return $this->provider_configuration;
}
public function setProviderConfiguration(array $provider_configuration) {
$plugin = $this
->getProviderPluginInstance();
if ($plugin instanceof ConfigurableInterface) {
$plugin
->setConfiguration($provider_configuration);
}
$this->provider_configuration = $provider_configuration;
}
public function getPluginCollections() {
return [
'provider_configuration' => $this
->getPluginCollection(),
];
}
public static function postLoad(EntityStorageInterface $storage, array &$entities) {
parent::postLoad($storage, $entities);
foreach ($entities as $entity) {
$entity
->addCacheTags([
self::cacheTagConstruct($entity
->getProviderPluginInstance()
->getPluginDefinition()['controlled_entity_type'], $entity
->getProviderPluginInstance()
->getPluginDefinition()['controlled_bundle']),
]);
}
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$this->base_provider_plugin = $this
->getProviderPluginInstance()
->getBaseId();
$this
->addCacheTags([
self::cacheTagConstruct($this
->getProviderPluginInstance()
->getPluginDefinition()['controlled_entity_type'], $this
->getProviderPluginInstance()
->getPluginDefinition()['controlled_bundle']),
]);
}
public function calculateDependencies() {
$return = parent::calculateDependencies();
$plugin_definition = $this
->getProviderPluginInstance()
->getPluginDefinition();
$controlled_entity_type = \Drupal::entityTypeManager()
->getDefinition($plugin_definition['controlled_entity_type']);
$dependency = $controlled_entity_type
->getBundleConfigDependency($plugin_definition['controlled_bundle']);
if ($dependency) {
$this
->addDependency($dependency['type'], $dependency['name']);
}
return $return;
}
public static function cacheTagConstruct($entity_type, $bundle) {
return 'synonyms:' . $entity_type . '.' . $bundle;
}
protected function getPluginCollection() {
if (!$this->pluginCollection && $this
->getProviderPlugin()) {
$this->pluginCollection = new ProviderPluginCollection(\Drupal::service('plugin.manager.synonyms_provider'), $this
->getProviderPlugin(), $this->provider_configuration);
}
return $this->pluginCollection;
}
protected function invalidateTagsOnSave($update) {
parent::invalidateTagsOnSave($update);
Cache::invalidateTags($this->cacheTags);
}
protected static function invalidateTagsOnDelete(EntityTypeInterface $entity_type, array $entities) {
$cacheability_metadata = new CacheableMetadata();
foreach ($entities as $entity) {
$cacheability_metadata
->addCacheableDependency($entity);
}
Cache::invalidateTags($cacheability_metadata
->getCacheTags());
}
}