Synonym.php in Synonyms 8
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\SynonymProviderPluginCollection;
use Drupal\synonyms\SynonymsProviderInterface;
class Synonym extends ConfigEntityBase implements SynonymInterface {
protected $provider_plugin;
protected $base_provider_plugin;
protected $provider_configuration = [];
protected $behavior;
protected $behavior_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 getBehaviorConfiguration() {
return $this->behavior_configuration;
}
public function setBehaviorConfiguration(array $behavior_configuration) {
$this->behavior_configuration = $behavior_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
->get('behavior'), $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->behavior = $this
->getProviderPluginInstance()
->getBehaviorService();
$this
->addCacheTags([
self::cacheTagConstruct($this->behavior, $this
->getProviderPluginInstance()
->getPluginDefinition()['controlled_entity_type'], $this
->getProviderPluginInstance()
->getPluginDefinition()['controlled_bundle']),
]);
}
public static function cacheTagConstruct($behavior, $entity_type, $bundle) {
return 'synonyms:' . $behavior . '.' . $entity_type . '.' . $bundle;
}
protected function getPluginCollection() {
if (!$this->pluginCollection && $this
->getProviderPlugin()) {
$this->pluginCollection = new SynonymProviderPluginCollection(\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());
}
}