LingotekProfileUsage.php in Lingotek Translation 8.2
File
src/LingotekProfileUsage.php
View source
<?php
namespace Drupal\lingotek;
use Drupal\config_translation\ConfigMapperManagerInterface;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class LingotekProfileUsage implements LingotekProfileUsageInterface {
use DeprecatedServicePropertyTrait;
protected $deprecatedProperties = [
'entityQuery' => 'entity.query',
];
public function __get($name) {
if (isset($this->deprecatedProperties[$name])) {
$service_name = $this->deprecatedProperties[$name];
$class_name = static::class;
@trigger_error("The property {$name} ({$service_name} service) is deprecated in {$class_name} and will be removed before Lingotek 9.x-1.0", E_USER_DEPRECATED);
return NULL;
}
}
protected $lingotekConfiguration;
protected $configMapperManager;
protected $entityTypeBundleInfo;
protected $entityTypeManager;
public function __construct(LingotekConfigurationServiceInterface $lingotek_configuration, $entity_query, ConfigMapperManagerInterface $config_mapper_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, EntityTypeManagerInterface $entity_type_manager = NULL) {
$this->lingotekConfiguration = $lingotek_configuration;
if (get_class($entity_query) === '\\Drupal\\Core\\Entity\\Query\\QueryFactory') {
@trigger_error('The entity.query service is deprecated. Pass the entity_type.manager service to LingotekProfileUsage::__construct instead. It is required before Lingotek 9.x-1.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
}
$this->configMapperManager = $config_mapper_manager;
if (!$entity_type_bundle_info) {
@trigger_error('The entity_type.bundle.info service must be passed to LingotekProfileUsage::__construct, it is required before Lingotek 9.x-1.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
}
$this->entityTypeBundleInfo = $entity_type_bundle_info;
if (!$entity_type_manager) {
@trigger_error('The entity_type.manager service must be passed to LingotekProfileUsage::__construct, it is required before Lingotek 9.x-1.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
$entity_type_manager = \Drupal::service('entity_type.manager');
}
$this->entityTypeManager = $entity_type_manager;
}
public function isUsedByContent(LingotekProfileInterface $profile) {
$entity_query = $this->entityTypeManager
->getStorage('lingotek_content_metadata')
->getQuery();
$entity_query
->condition('profile', $profile
->id());
$result = $entity_query
->count()
->execute();
$used = $result > 0 ? LingotekProfileUsageInterface::USED_BY_CONTENT : LingotekProfileUsageInterface::UNUSED;
return $used;
}
public function isUsedByConfig(LingotekProfileInterface $profile) {
$mappers = $this->configMapperManager
->getMappers();
$used = LingotekProfileUsageInterface::UNUSED;
foreach ($mappers as $plugin_id => $mapper) {
$config_profile = $this->lingotekConfiguration
->getConfigProfile($plugin_id, FALSE);
if ($config_profile !== NULL && $config_profile
->id() === $profile
->id()) {
$used |= LingotekProfileUsageInterface::USED_BY_CONFIG;
}
}
if ($used !== LingotekProfileUsageInterface::USED_BY_CONFIG) {
$entity_query = $this->entityTypeManager
->getStorage('lingotek_config_metadata')
->getQuery();
$entity_query
->condition('profile', $profile
->id());
$result = $entity_query
->count()
->execute();
$used = $result > 0 ? LingotekProfileUsageInterface::USED_BY_CONFIG : LingotekProfileUsageInterface::UNUSED;
}
return $used;
}
public function isUsedByContentSettings(LingotekProfileInterface $profile) {
$entity_types = $this->lingotekConfiguration
->getEnabledEntityTypes();
$used = LingotekProfileUsageInterface::UNUSED;
foreach ($entity_types as $entity_type_id => $entity_type_definition) {
$bundles = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
foreach ($bundles as $bundle_id => $bundle_definition) {
$config_profile = $this->lingotekConfiguration
->getDefaultProfileId($entity_type_id, $bundle_id);
if ($config_profile === $profile
->id()) {
$used |= LingotekProfileUsageInterface::USED_BY_SETTINGS;
}
}
}
return $used;
}
}