View source
<?php
namespace Drupal\entity_legal\Entity;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_legal\EntityLegalDocumentInterface;
use Drupal\entity_legal\EntityLegalDocumentVersionInterface;
use Drupal\entity_legal\Form\EntityLegalDocumentAcceptanceForm;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class EntityLegalDocument extends ConfigEntityBundleBase implements EntityLegalDocumentInterface {
protected $id;
protected $label;
protected $require_signup = FALSE;
protected $require_existing = FALSE;
protected $settings = [];
public static function preCreate(EntityStorageInterface $storage, array &$values) {
parent::preCreate($storage, $values);
if (empty($values['settings']['title_pattern'])) {
$values['settings']['title_pattern'] = '[entity_legal_document:label]';
}
}
public function delete() {
if (!$this
->isNew()) {
$versions = $this
->getAllVersions();
foreach ($versions as $version) {
$version
->delete();
}
}
parent::delete();
}
public function getAcceptanceForm() {
$form = \Drupal::classResolver()
->getInstanceFromDefinition(EntityLegalDocumentAcceptanceForm::class);
$form
->setDocument($this);
return \Drupal::formBuilder()
->getForm($form);
}
public function getAllVersions() {
$query = \Drupal::entityQuery(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME)
->condition('document_name', $this
->id());
$results = $query
->execute();
if (!empty($results)) {
return \Drupal::entityTypeManager()
->getStorage(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME)
->loadMultiple($results);
}
else {
return [];
}
}
public function getPublishedVersion() {
$storage = $this
->entityTypeManager()
->getStorage(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME);
$ids = $storage
->getQuery()
->condition('document_name', $this
->id())
->condition('published', TRUE)
->execute();
if (!$ids) {
return FALSE;
}
$id = reset($ids);
return $storage
->load($id);
}
public function setPublishedVersion(EntityLegalDocumentVersionInterface $version_entity) {
if (!$version_entity
->isNew()) {
$unchanged_version = $this
->entityTypeManager()
->getStorage(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME)
->loadUnchanged($version_entity
->id());
if ($unchanged_version
->isPublished()) {
return TRUE;
}
}
if ($version_entity
->bundle() != $this
->id()) {
return FALSE;
}
if ($actual_published_version = $this
->getPublishedVersion()) {
$actual_published_version
->unpublish()
->save();
}
$version_entity
->publish()
->save();
return TRUE;
}
public function getAcceptanceLabel() {
$label = '';
$published_version = $this
->getPublishedVersion();
if ($published_version) {
$label = $published_version
->get('acceptance_label')->value;
}
$token_service = \Drupal::service('token');
$label = $token_service
->replace($label, [
ENTITY_LEGAL_DOCUMENT_ENTITY_NAME => $this,
]);
return Xss::filter($label);
}
public function toUrl($rel = 'canonical', array $options = []) {
$options += [
'language' => NULL,
];
return parent::toUrl($rel, $options);
}
public function userMustAgree($new_user = FALSE, AccountInterface $account = NULL) {
if (!$this
->getPublishedVersion()) {
return FALSE;
}
if (empty($account)) {
$account = \Drupal::currentUser();
}
if ($new_user) {
return !empty($this->require_signup);
}
else {
return !empty($this->require_existing) && $account
->hasPermission($this
->getPermissionExistingUser());
}
}
public function userHasAgreed(AccountInterface $account = NULL) {
if (empty($account)) {
$account = \Drupal::currentUser();
}
return count($this
->getAcceptances($account)) > 0;
}
public function getAcceptances(AccountInterface $account = NULL, $published = TRUE) {
$acceptances = [];
$versions = [];
if ($published) {
$versions[] = $this
->getPublishedVersion();
}
else {
$versions = $this
->getAllVersions();
}
foreach ($versions as $version) {
$acceptances += $version
->getAcceptances($account);
}
return $acceptances;
}
public function getPermissionView() {
return 'legal view ' . $this
->id();
}
public function getPermissionExistingUser() {
return 'legal re-accept ' . $this
->id();
}
public function getAcceptanceDeliveryMethod($new_user = FALSE) {
$setting_group = $new_user ? 'new_users' : 'existing_users';
return isset($this
->get('settings')[$setting_group]['require_method']) ? $this
->get('settings')[$setting_group]['require_method'] : FALSE;
}
public function save() {
$status = parent::save();
if ($status == SAVED_NEW && !\Drupal::isConfigSyncing()) {
$field = FieldConfig::loadByName('entity_legal_document_version', $this
->id(), 'entity_legal_document_text');
if (empty($field)) {
FieldConfig::create([
'field_storage' => FieldStorageConfig::loadByName('entity_legal_document_version', 'entity_legal_document_text'),
'bundle' => $this
->id(),
'label' => 'Document text',
'settings' => [
'display_summary' => FALSE,
],
])
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('entity_legal_document_version', $this
->id(), 'default')
->setComponent('entity_legal_document_text', [
'type' => 'text_textarea_with_summary',
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('entity_legal_document_version', $this
->id(), 'default')
->setComponent('entity_legal_document_text', [
'label' => 'hidden',
'type' => 'text_default',
])
->save();
}
}
else {
Cache::invalidateTags([
"entity_legal_document:{$this->id()}",
]);
}
return $status;
}
}