View source
<?php
namespace Drupal\transaction;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityInterface;
class TransactionTypeStorage extends ConfigEntityStorage {
protected $entityTypeManager;
protected $fieldManager;
protected $routeBuilder;
protected $cacheTagsInvalidator;
protected $cacheDiscovery;
public function __construct(EntityTypeInterface $entity_type, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, MemoryCacheInterface $memory_cache = NULL, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $field_manager, RouteBuilderInterface $route_builder, CacheTagsInvalidatorInterface $cache_tags_invalidator, CacheBackendInterface $cache_discovery) {
parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager, $memory_cache);
$this->entityTypeManager = $entity_type_manager;
$this->fieldManager = $field_manager;
$this->routeBuilder = $route_builder;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
$this->cacheDiscovery = $cache_discovery;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('config.factory'), $container
->get('uuid'), $container
->get('language_manager'), $container
->get('entity.memory_cache'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('router.builder'), $container
->get('cache_tags.invalidator'), $container
->get('cache.discovery'));
}
protected function doPostSave(EntityInterface $entity, $update) {
parent::doPostSave($entity, $update);
if ($this
->updateLocalTask($entity, $entity
->getOption('local_task', FALSE))) {
$this
->clearLocalTaskCache();
}
}
protected function doDelete($entities) {
parent::doDelete($entities);
$changes = FALSE;
foreach ($entities as $entity) {
$changes = $changes || $this
->updateLocalTask($entity, FALSE);
}
if ($changes) {
$this
->clearLocalTaskCache();
}
}
protected function updateLocalTask(TransactionTypeInterface $transaction_type, $has_tab) {
$config = $this->configFactory
->getEditable('transaction.settings');
$tabs = $config
->get('tabs');
$option_id = $transaction_type
->id() . '-' . $transaction_type
->getTargetEntityTypeId();
$has_config = array_search($option_id, $tabs);
if (is_numeric($has_config) != $has_tab) {
if ($has_tab) {
$tabs[] = $option_id;
}
else {
unset($tabs[$has_config]);
}
$config
->set('tabs', $tabs);
$config
->save();
return TRUE;
}
return FALSE;
}
protected function clearLocalTaskCache() {
$this->entityTypeManager
->clearCachedDefinitions();
$this->fieldManager
->clearCachedFieldDefinitions();
$this->cacheTagsInvalidator
->invalidateTags([
'block_view',
]);
$this->cacheDiscovery
->invalidateAll();
$this->routeBuilder
->rebuild();
}
}