WorkspacePointer.php in Workspace 8
File
src/Entity/WorkspacePointer.php
View source
<?php
namespace Drupal\workspace\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Drupal\replication\ReplicationTask\ReplicationTaskInterface;
use Drupal\workspace\WorkspacePointerInterface;
class WorkspacePointer extends ContentEntityBase implements WorkspacePointerInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
}
public function label() {
$label = parent::label();
if (empty($label) && !empty($this
->getWorkspace())) {
$label = $this
->getWorkspace()
->label();
}
return $label;
}
public function getName() {
return $this
->get('name')->value;
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function setWorkspace(WorkspaceInterface $workspace) {
$this
->set('workspace_pointer', $workspace
->id());
return $this;
}
public function setWorkspaceId($workspace_id) {
$this
->set('workspace_pointer', $workspace_id);
return $this;
}
public function getWorkspace() {
$ws = $this
->get('workspace_pointer')->entity;
return $ws;
}
public function getWorkspaceId() {
return $this
->get('workspace_pointer')->target_id;
}
public function setWorkspaceAvailable($available = TRUE) {
$this
->set('workspace_available', $available);
return $this;
}
public function getWorkspaceAvailable() {
return (bool) $this
->get('workspace_available')->value;
}
public function generateReplicationId(WorkspacePointerInterface $target, ReplicationTaskInterface $task = NULL) {
$request = \Drupal::request();
$uuid = MD5($request
->getHost() . $request
->getPort());
$source_name = $this
->label();
if ($this
->getWorkspace() instanceof WorkspaceInterface) {
$source_name = $this
->getWorkspace()
->getMachineName();
}
$target_name = $target
->label();
if ($target
->getWorkspace() instanceof WorkspaceInterface) {
$target_name = $target
->getWorkspace()
->getMachineName();
}
if ($task) {
return \md5($uuid . $source_name . $target_name . var_export($task
->getDocIds(), TRUE) . ($task
->getCreateTarget() ? '1' : '0') . ($task
->getContinuous() ? '1' : '0') . $task
->getFilter() . '' . $task
->getStyle() . var_export($task
->getHeartbeat(), TRUE));
}
return \md5($uuid . $source_name . $target_name);
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Workspace pointer entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Workspace pointer entity.'))
->setReadOnly(TRUE);
$fields['revision_id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The revision ID of the workspace pointer entity.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Workspace pointer entity.'))
->setSettings([
'max_length' => 512,
'text_processing' => 0,
])
->setRevisionable(TRUE)
->setDefaultValue('')
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'))
->setRevisionable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'))
->setRevisionable(TRUE);
$fields['workspace_pointer'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Workspace'))
->setDescription(t('A reference to the workspace'))
->setSetting('target_type', 'workspace')
->setRevisionable(TRUE);
$fields['workspace_available'] = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Workspace available'))
->setDescription(t('Keeps the availability of the referenced ' . 'workspace, this flag might not be accurate, the availability should ' . 'be checked regularly (it is checked on cron run).'))
->setRevisionable(TRUE)
->setDefaultValue(TRUE);
return $fields;
}
public static function loadFromWorkspace(WorkspaceInterface $workspace) {
$pointers = \Drupal::service('entity_type.manager')
->getStorage('workspace_pointer')
->loadByProperties([
'workspace_pointer' => $workspace
->id(),
]);
return reset($pointers);
}
}