ScheduledTransition.php in Scheduled Transitions 2.x
File
src/Entity/ScheduledTransition.php
View source
<?php
declare (strict_types=1);
namespace Drupal\scheduled_transitions\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\scheduled_transitions\ScheduledTransitionsUtility;
use Drupal\user\UserInterface;
use Drupal\workflows\WorkflowInterface;
class ScheduledTransition extends ContentEntityBase implements ScheduledTransitionInterface {
public const OPTION_RECREATE_NON_DEFAULT_HEAD = 'recreate_non_default_head';
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) : array {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['entity'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(\t('Entity'))
->setDescription(\t('The entity this scheduled transition is for.'))
->setRequired(TRUE)
->setCardinality(1);
$fields['entity_revision_id'] = BaseFieldDefinition::create('integer')
->setLabel(\t('Content entity revision ID'))
->setDescription(\t('The revision ID of the entity this scheduled transition is for.'))
->setRequired(TRUE)
->setCardinality(1);
$fields['entity_revision_langcode'] = BaseFieldDefinition::create('language')
->setLabel(\t('Content entity revision language'))
->setDescription(\t('The revision language of the entity this scheduled transition is for.'))
->setRequired(FALSE)
->setCardinality(1);
$fields['author'] = BaseFieldDefinition::create('entity_reference')
->setLabel(\t('Authored by'))
->setDescription(\t('The user who initiated the scheduled transition.'))
->setSetting('target_type', 'user');
$fields['workflow'] = BaseFieldDefinition::create('entity_reference')
->setLabel(\t('Workflow'))
->setDescription(\t('The workflow of the state.'))
->setSetting('target_type', 'workflow')
->setRequired(TRUE);
$fields['moderation_state'] = BaseFieldDefinition::create('string')
->setLabel(\t('Moderation state'))
->setDescription(\t('The new state of the content.'))
->setRequired(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(\t('Authored on'))
->setDescription(\t('The time that the scheduled transition was created.'));
$fields['transition_on'] = BaseFieldDefinition::create('timestamp')
->setLabel(\t('Transition on'))
->setDescription(\t('The time scheduled transition should happen.'))
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
]);
$fields['locked_on'] = BaseFieldDefinition::create('timestamp')
->setLabel(\t('Locked on'))
->setDescription(\t('The time a job was created to process the transition.'));
$fields['options'] = BaseFieldDefinition::create('map')
->setLabel(t('Options'))
->setDescription(t('Arbitrary settings for scheduled transition.'));
return $fields;
}
public static function createFrom(WorkflowInterface $workflow, string $state, RevisionableInterface $revision, \DateTimeInterface $dateTime, AccountInterface $author) {
$scheduledTransition = static::create();
$scheduledTransition
->setState($workflow, $state)
->setEntity($revision)
->setTransitionDate($dateTime)
->setAuthor($author);
return $scheduledTransition;
}
public function getEntity() : ?EntityInterface {
return $this
->get('entity')->entity;
}
public function setEntity(RevisionableInterface $revision) {
$this
->set('entity', $revision);
$this
->setEntityRevisionId($revision
->getRevisionId());
$this
->setEntityRevisionLanguage($revision
->language()
->getId());
return $this;
}
public function getEntityRevisionId() {
return $this
->get('entity_revision_id')->value;
}
public function setEntityRevisionId($revisionId) {
assert(is_int($revisionId) || is_string($revisionId));
$this
->set('entity_revision_id', $revisionId);
return $this;
}
public function getEntityRevisionLanguage() : ?string {
return $this
->get('entity_revision_langcode')->value;
}
public function setEntityRevisionLanguage(string $langCode) {
$this->entity_revision_langcode = $langCode;
return $this;
}
public function getAuthor() : ?UserInterface {
return $this
->get('author')->entity;
}
public function setAuthor(AccountInterface $author) {
$this->author = $author
->id();
return $this;
}
public function getWorkflow() : ?WorkflowInterface {
return $this
->get('workflow')->entity;
}
public function setState(WorkflowInterface $workflow, string $state) {
$this->workflow = $workflow
->id();
$this->moderation_state = $state;
return $this;
}
public function getState() : ?string {
return $this
->get('moderation_state')->value;
}
public function getCreatedTime() : int {
return $this
->get('created')->value;
}
public function getTransitionDate() : \DateTimeInterface {
return new \DateTime('@' . $this
->getTransitionTime());
}
public function getTransitionTime() : int {
return (int) $this
->get('transition_on')->value;
}
public function setTransitionDate(\DateTimeInterface $dateTime) {
$this
->set('transition_on', $dateTime
->getTimestamp());
return $this;
}
public function setTransitionTime(int $time) {
$this
->set('transition_on', $time);
return $this;
}
public function setLockedOn(int $time) {
$this
->set('locked_on', $time);
return $this;
}
public function getOptions() : array {
$options = $this
->get('options')
->first();
if (isset($options)) {
return $options
->getValue();
}
return [];
}
public function setOptions(array $options) {
$this->options = $options;
return $this;
}
public function getCacheTagsToInvalidate() : array {
$tags = parent::getCacheTagsToInvalidate();
$entity = $this
->getEntity();
if ($entity) {
$tags[] = ScheduledTransitionsUtility::createScheduledTransitionsCacheTag($entity);
}
return $tags;
}
protected function invalidateTagsOnSave($update) {
parent::invalidateTagsOnSave($update);
$entity = $this
->getEntity();
if ($entity) {
$tags[] = ScheduledTransitionsUtility::createScheduledTransitionsCacheTag($entity);
}
Cache::invalidateTags($tags);
}
}