ContentTranslationManager.php in Zircon Profile 8
File
core/modules/content_translation/src/ContentTranslationManager.php
View source
<?php
namespace Drupal\content_translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
class ContentTranslationManager implements ContentTranslationManagerInterface {
protected $entityManager;
protected $updatesManager;
public function __construct(EntityManagerInterface $manager, ContentTranslationUpdatesManager $updates_manager) {
$this->entityManager = $manager;
$this->updatesManager = $updates_manager;
}
function getTranslationHandler($entity_type_id) {
return $this->entityManager
->getHandler($entity_type_id, 'translation');
}
public function getTranslationMetadata(EntityInterface $translation) {
$entity_type = $translation
->getEntityType();
$class = $entity_type
->get('content_translation_metadata');
return new $class($translation, $this
->getTranslationHandler($entity_type
->id()));
}
public function isSupported($entity_type_id) {
$entity_type = $this->entityManager
->getDefinition($entity_type_id);
return $entity_type
->isTranslatable() && ($entity_type
->hasLinkTemplate('drupal:content-translation-overview') || $entity_type
->get('content_translation_ui_skip'));
}
public function getSupportedEntityTypes() {
$supported_types = array();
foreach ($this->entityManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($this
->isSupported($entity_type_id)) {
$supported_types[$entity_type_id] = $entity_type;
}
}
return $supported_types;
}
public function setEnabled($entity_type_id, $bundle, $value) {
$config = $this
->loadContentLanguageSettings($entity_type_id, $bundle);
$config
->setThirdPartySetting('content_translation', 'enabled', $value)
->save();
$entity_type = $this->entityManager
->getDefinition($entity_type_id);
$this->updatesManager
->updateDefinitions(array(
$entity_type_id => $entity_type,
));
}
public function isEnabled($entity_type_id, $bundle = NULL) {
$enabled = FALSE;
if ($this
->isSupported($entity_type_id)) {
$bundles = !empty($bundle) ? array(
$bundle,
) : array_keys($this->entityManager
->getBundleInfo($entity_type_id));
foreach ($bundles as $bundle) {
$config = $this
->loadContentLanguageSettings($entity_type_id, $bundle);
if ($config
->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
$enabled = TRUE;
break;
}
}
}
return $enabled;
}
protected function loadContentLanguageSettings($entity_type_id, $bundle) {
if ($entity_type_id == NULL || $bundle == NULL) {
return NULL;
}
$config = $this->entityManager
->getStorage('language_content_settings')
->load($entity_type_id . '.' . $bundle);
if ($config == NULL) {
$config = $this->entityManager
->getStorage('language_content_settings')
->create([
'target_entity_type_id' => $entity_type_id,
'target_bundle' => $bundle,
]);
}
return $config;
}
}