LingotekContentMetadata.php in Lingotek Translation 3.5.x
File
src/Entity/LingotekContentMetadata.php
View source
<?php
namespace Drupal\lingotek\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\lingotek\Plugin\Field\FieldType\LingotekTranslationSourceField;
class LingotekContentMetadata extends ContentEntityBase {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['content_entity_type_id'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Content entity type ID'))
->setDescription(new TranslatableMarkup('The ID of the content entity type this Lingotek status is for.'))
->setRequired(TRUE);
$fields['content_entity_id'] = BaseFieldDefinition::create('integer')
->setLabel(new TranslatableMarkup('Content entity ID'))
->setDescription(new TranslatableMarkup('The ID of the content entity this Lingotek status is for.'))
->setRequired(TRUE);
$fields['document_id'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Lingotek document id'))
->setDescription(new TranslatableMarkup('The Lingotek document id.'));
$fields['hash'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Lingotek hash'))
->setDescription(new TranslatableMarkup('A hash of the Lingotek saved entity data, required for checking for changes.'));
$fields['profile'] = BaseFieldDefinition::create('entity_reference')
->setLabel(new TranslatableMarkup('Lingotek profile'))
->setDescription(new TranslatableMarkup('The Lingotek profile defining this translation.'))
->setSetting('target_type', 'lingotek_profile');
$fields['translation_source'] = BaseFieldDefinition::create('language')
->setComputed(TRUE)
->setClass(LingotekTranslationSourceField::class)
->setLabel(new TranslatableMarkup('Lingotek translation source'))
->setDescription(new TranslatableMarkup('The source language from which this translation was created.'))
->setDefaultValue(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->setTranslatable(TRUE);
$fields['translation_status'] = BaseFieldDefinition::create('lingotek_language_key_value')
->setLabel(new TranslatableMarkup('Lingotek translation status'))
->setDescription(new TranslatableMarkup('The status of the source in case of being the source translation, or the status of the translation.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$fields['job_id'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Lingotek job id'))
->setDescription(new TranslatableMarkup('The Lingotek job id.'));
$fields['updated_timestamp'] = BaseFieldDefinition::create('timestamp')
->setLabel(new TranslatableMarkup('Updated timestamp'))
->setDescription(new TranslatableMarkup('The last time document was updated.'));
$fields['uploaded_timestamp'] = BaseFieldDefinition::create('timestamp')
->setLabel(new TranslatableMarkup('Initial upload'))
->setDescription(new TranslatableMarkup('The time of the initial upload.'));
return $fields;
}
public static function loadByTargetId($target_entity_type_id, $target_id) {
$metadata = NULL;
if ($target_entity_type_id == NULL || $target_id == NULL) {
return NULL;
}
$entity_query = \Drupal::entityQuery('lingotek_content_metadata');
$entity_query
->condition('content_entity_type_id', $target_entity_type_id)
->condition('content_entity_id', $target_id);
$result = $entity_query
->execute();
if (!empty($result)) {
$metadata = self::load(reset($result));
}
if ($metadata === NULL) {
$metadata = new static([
'content_entity_type_id' => $target_entity_type_id,
'content_entity_id' => $target_id,
], 'lingotek_content_metadata');
}
return $metadata;
}
public static function loadByDocumentId($document_id) {
$metadata = NULL;
if ($document_id !== NULL) {
$entity_query = \Drupal::entityQuery('lingotek_content_metadata');
$entity_query
->condition('document_id', $document_id);
$result = $entity_query
->execute();
if (!empty($result)) {
$metadata = self::load(reset($result));
}
}
return $metadata;
}
public static function getAllLocalDocumentIds() {
return \Drupal::database()
->select('lingotek_metadata', 'lcm')
->fields('lcm', [
'document_id',
])
->execute()
->fetchCol(0);
}
public function getDocumentId() {
return $this->document_id->value;
}
public function setDocumentId($document_id) {
$this->document_id->value = $document_id;
}
public function getProfile() {
return $this->profile->target_id;
}
public function setProfile($profile_id) {
$this->profile->target_id = $profile_id;
}
public function setEntity(EntityInterface $entity) {
$this->content_entity_type_id->value = $entity
->getEntityTypeId();
$this->content_entity_id->value = $entity
->id();
return $this;
}
public function getContentEntityTypeId() {
return $this->content_entity_type_id->value;
}
public function getContentEntityId() {
return $this->content_entity_id->value;
}
public function getJobId() {
return $this->job_id->value;
}
public function setContentEntityTypeId($value) {
$this->content_entity_type_id = $value;
return $this;
}
public function setContentEntityId($value) {
$this->content_entity_id = $value;
return $this;
}
public function setJobId($value) {
$this->job_id = $value;
return $this;
}
public function setLastUpdated($timestamp) {
$this->updated_timestamp->value = $timestamp;
return $this;
}
public function getLastUpdated() {
return $this->updated_timestamp->value;
}
public function setLastUploaded($timestamp) {
$this->uploaded_timestamp->value = $timestamp;
return $this;
}
public function getLastUploaded() {
return $this->uploaded_timestamp->value;
}
}