LingotekConfigMetadata.php in Lingotek Translation 8
File
src/Entity/LingotekConfigMetadata.php
View source
<?php
namespace Drupal\lingotek\Entity;
use Drupal\config_translation\ConfigMapperInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\lingotek\LingotekConfigMetadataInterface;
class LingotekConfigMetadata extends ConfigEntityBase implements LingotekConfigMetadataInterface {
protected $config_name;
protected $document_id;
protected $source_status = [];
protected $target_status = [];
protected $hash = NULL;
public function getDocumentId() {
return $this->document_id;
}
public function setDocumentId($document_id) {
$this->document_id = $document_id;
return $this;
}
public function getSourceStatus() {
return $this->source_status;
}
public function setSourceStatus(array $source_status) {
$this->source_status = $source_status;
return $this;
}
public function getTargetStatus() {
return $this->target_status;
}
public function setTargetStatus(array $target_status) {
$this->target_status = $target_status;
return $this;
}
public function getHash() {
return $this->hash;
}
public function setHash($hash) {
$this->hash = $hash;
return $this;
}
public static function loadByConfigName($config_name) {
if ($config_name == NULL) {
return NULL;
}
$config = \Drupal::entityManager()
->getStorage('lingotek_config_metadata')
->load($config_name);
if ($config == NULL) {
$config = LingotekConfigMetadata::create([
'config_name' => $config_name,
]);
}
return $config;
}
public function id() {
return $this->config_name;
}
public function calculateDependencies() {
parent::calculateDependencies();
$dependency_name = $this
->getDependencyName();
$this
->addDependency('config', $dependency_name);
return $this;
}
public function getDependencyName() {
list($entity_type, $entity_id) = explode('.', $this->config_name, 2);
if ($entity_type === 'field_config') {
$field_config = FieldConfig::load($entity_id);
$value = $field_config
->getConfigDependencyName();
}
else {
if ($this
->entityManager()
->hasDefinition($entity_type)) {
$storage = $this
->entityManager()
->getStorage($entity_type);
$entity = $storage
->load($entity_id);
$value = $entity ? $entity
->getConfigDependencyName() : $this->config_name;
}
else {
$value = $this->config_name;
}
}
return $value;
}
public function getConfigMapper() {
$mapper = NULL;
$mappers = \Drupal::service('plugin.manager.config_translation.mapper')
->getMappers();
$name = $this
->getDependencyName();
$config_mapper_id = $this
->getMapperIdForName($name);
if (isset($mappers[$config_mapper_id])) {
$mapper = $mappers[$config_mapper_id];
}
else {
list($entity_type, $entity_id) = explode('.', $this->config_name, 2);
if (isset($mappers[$entity_type])) {
$storage = $this
->entityManager()
->getStorage($entity_type);
$entity = $storage
->load($entity_id);
$mapper = clone $mappers[$entity_type];
$mapper
->setEntity($entity);
}
}
return $mapper;
}
protected function getMapperIdForName($name) {
$mapper_id = NULL;
$config_mappers = \Drupal::service('plugin.manager.config_translation.mapper')
->getMappers();
foreach ($config_mappers as $config_mapper_id => $config_mapper) {
$names = $config_mapper
->getConfigNames();
if (in_array($name, $names)) {
$mapper_id = $config_mapper_id;
break;
}
}
return $mapper_id;
}
}