EntityLegalDocumentVersion.php in Entity Legal 8.2
File
src/Entity/EntityLegalDocumentVersion.php
View source
<?php
namespace Drupal\entity_legal\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_legal\EntityLegalDocumentVersionInterface;
class EntityLegalDocumentVersion extends ContentEntityBase implements EntityLegalDocumentVersionInterface {
use EntityChangedTrait;
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The entity ID of this document.'))
->setReadOnly(TRUE)
->setSetting('max_length', 64)
->setSetting('unsigned', TRUE)
->setDefaultValueCallback('Drupal\\entity_legal\\Entity\\EntityLegalDocumentVersion::getDefaultName');
$fields['published'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Published'))
->setDescription(t('If this is the published version of the legal document.'))
->setRequired(TRUE)
->setDefaultValue(FALSE)
->setCardinality(1)
->setConstraints([
'SingleLegalDocumentPublishedVersion' => [],
])
->setInitialValue(FALSE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language'))
->setDescription(t('The document version language code.'))
->setTranslatable(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The entity UUID of this document'))
->setReadOnly(TRUE);
$fields['document_name'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Form ID'))
->setDescription(t('The name of the document this version is bound to.'))
->setSetting('target_type', ENTITY_LEGAL_DOCUMENT_ENTITY_NAME)
->setRequired(TRUE);
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setDescription(t('The title of the document.'))
->setTranslatable(TRUE)
->setRequired(TRUE);
$fields['acceptance_label'] = BaseFieldDefinition::create('string')
->setLabel(t('Acceptance label'))
->setTranslatable(TRUE)
->setRequired(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The date the document was created.'))
->setTranslatable(TRUE)
->setRequired(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The date the document was changed.'))
->setTranslatable(TRUE)
->setRequired(TRUE);
return $fields;
}
public function delete() {
if (!$this
->isNew()) {
$acceptances = $this
->getAcceptances();
foreach ($acceptances as $acceptance) {
$acceptance
->delete();
}
}
parent::delete();
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public static function getDefaultName(EntityLegalDocumentVersionInterface $entity) {
return $entity
->bundle() . '_' . time();
}
public function getFormattedDate($type = 'changed') {
switch ($type) {
case 'changed':
return \Drupal::service('date.formatter')
->format($this
->getChangedTime());
case 'created':
return \Drupal::service('date.formatter')
->format($this
->getCreatedTime());
}
}
public function getAcceptances(AccountInterface $account = NULL) {
$query = \Drupal::entityQuery(ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME)
->condition('document_version_name', $this
->id());
if ($account) {
$query
->condition('uid', $account
->id());
}
$results = $query
->execute();
if (!empty($results)) {
return \Drupal::entityTypeManager()
->getStorage(ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME)
->loadMultiple($results);
}
return [];
}
public function getDocument() {
return \Drupal::entityTypeManager()
->getStorage(ENTITY_LEGAL_DOCUMENT_ENTITY_NAME)
->load($this
->bundle());
}
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if (in_array($rel, [
'canonical',
'entity_hierarchy_reorder',
'token-devel',
'drupal:content-translation-overview',
'drupal:content-translation-add',
'drupal:content-translation-edit',
'drupal:content-translation-delete',
])) {
$uri_route_parameters['entity_legal_document'] = $this
->bundle();
}
return $uri_route_parameters;
}
public function isPublished() {
return (bool) $this
->get('published')->value;
}
public function publish() {
$this
->set('published', TRUE);
return $this;
}
public function unpublish() {
$this
->set('published', FALSE);
return $this;
}
}