LingotekContentTranslationHandler.php in Lingotek Translation 8
File
src/LingotekContentTranslationHandler.php
View source
<?php
namespace Drupal\lingotek;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\DataReferenceDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LingotekContentTranslationHandler implements LingotekContentTranslationHandlerInterface, EntityHandlerInterface {
use DependencySerializationTrait;
protected $entityTypeId;
protected $entityType;
protected $languageManager;
protected $fieldStorageDefinitions;
public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager) {
$this->entityTypeId = $entity_type
->id();
$this->entityType = $entity_type;
$this->languageManager = $language_manager;
$this->fieldStorageDefinitions = $entity_manager
->getLastInstalledFieldStorageDefinitions($this->entityTypeId);
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('language_manager'), $container
->get('entity.manager'));
}
public function getFieldDefinitions() {
$definitions = array();
$definitions['lingotek_document_id'] = BaseFieldDefinition::create('string')
->setLabel(t('Lingotek document id'))
->setDescription(t('The Lingotek document id.'));
$definitions['lingotek_hash'] = BaseFieldDefinition::create('string')
->setLabel(t('Lingotek hash'))
->setDescription(t('A hash of the Lingotek saved entity data, required for checking for changes.'));
$definitions['lingotek_profile'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Lingotek profile'))
->setDescription(t('The Lingotek profile defining this translation.'))
->setSetting('target_type', 'lingotek_profile');
$definitions['lingotek_translation_source'] = BaseFieldDefinition::create('language')
->setLabel(t('Lingotek translation source'))
->setDescription(t('The source language from which this translation was created.'))
->setDefaultValue(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->setTranslatable(TRUE);
$definitions['lingotek_translation_status'] = BaseFieldDefinition::create('lingotek_language_key_value')
->setLabel(t('Lingotek translation status'))
->setDescription(t('The status of the source in case of being the source translation, or the status of the translation.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
if (!$this
->hasCreatedTime()) {
$definitions['lingotek_translation_created'] = BaseFieldDefinition::create('created')
->setLabel(t('Lingotek translation created time'))
->setDescription(t('The Unix timestamp when the translation was created.'))
->setTranslatable(TRUE);
}
if (!$this
->hasChangedTime()) {
$definitions['lingotek_translation_changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Translation changed time'))
->setDescription(t('The Unix timestamp when the translation was most recently saved.'))
->setTranslatable(TRUE);
}
return $definitions;
}
protected function hasCreatedTime() {
return $this
->checkFieldStorageDefinitionTranslatability('created');
}
protected function hasChangedTime() {
return $this->entityType
->isSubclassOf('Drupal\\Core\\Entity\\EntityChangedInterface') && $this
->checkFieldStorageDefinitionTranslatability('changed');
}
protected function checkFieldStorageDefinitionTranslatability($field_name) {
return array_key_exists($field_name, $this->fieldStorageDefinitions) && $this->fieldStorageDefinitions[$field_name]
->isTranslatable();
}
}