ModerationStateFieldItemList.php in Drupal 8
File
core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
View source
<?php
namespace Drupal\content_moderation\Plugin\Field;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
class ModerationStateFieldItemList extends FieldItemList {
use ComputedItemListTrait {
get as traitGet;
}
protected function computeValue() {
$moderation_state = $this
->getModerationStateId();
if ($moderation_state) {
$this->list[0] = $this
->createItem(0, $moderation_state);
}
}
protected function getModerationStateId() {
$entity = $this
->getEntity();
$moderation_info = \Drupal::service('content_moderation.moderation_information');
if (!$moderation_info
->shouldModerateEntitiesOfBundle($entity
->getEntityType(), $entity
->bundle())) {
return NULL;
}
if (!$entity
->isNew() && ($content_moderation_state = $this
->loadContentModerationStateRevision($entity))) {
return $content_moderation_state->moderation_state->value;
}
$workflow = $moderation_info
->getWorkFlowForEntity($entity);
return $workflow ? $workflow
->getTypePlugin()
->getInitialState($entity)
->id() : NULL;
}
protected function loadContentModerationStateRevision(ContentEntityInterface $entity) {
$moderation_info = \Drupal::service('content_moderation.moderation_information');
$content_moderation_storage = \Drupal::entityTypeManager()
->getStorage('content_moderation_state');
$revisions = $content_moderation_storage
->getQuery()
->condition('content_entity_type_id', $entity
->getEntityTypeId())
->condition('content_entity_id', $entity
->id())
->condition('content_entity_revision_id', $entity
->isNewRevision() ? $entity
->getLoadedRevisionId() : $entity
->getRevisionId())
->condition('workflow', $moderation_info
->getWorkflowForEntity($entity)
->id())
->condition('langcode', $entity
->language()
->getId())
->allRevisions()
->sort('revision_id', 'DESC')
->execute();
if (empty($revisions)) {
return NULL;
}
$content_moderation_state = $content_moderation_storage
->loadRevision(key($revisions));
if ($entity
->getEntityType()
->hasKey('langcode')) {
$langcode = $entity
->language()
->getId();
if (!$content_moderation_state
->hasTranslation($langcode)) {
$content_moderation_state
->addTranslation($langcode, $content_moderation_state
->toArray());
}
if ($content_moderation_state
->language()
->getId() !== $langcode) {
$content_moderation_state = $content_moderation_state
->getTranslation($langcode);
}
}
return $content_moderation_state;
}
public function get($index) {
if ($index !== 0) {
throw new \InvalidArgumentException('An entity can not have multiple moderation states at the same time.');
}
return $this
->traitGet($index);
}
public function onChange($delta) {
$this
->updateModeratedEntity($this->list[$delta]->value);
parent::onChange($delta);
}
public function setValue($values, $notify = TRUE) {
parent::setValue($values, $notify);
$this->valueComputed = TRUE;
if (isset($this->list[0]) && $notify) {
$this
->updateModeratedEntity($this->list[0]->value);
}
}
protected function updateModeratedEntity($moderation_state_id) {
$entity = $this
->getEntity();
$content_moderation_info = \Drupal::service('content_moderation.moderation_information');
$workflow = $content_moderation_info
->getWorkflowForEntity($entity);
if ($workflow && $workflow
->getTypePlugin()
->hasState($moderation_state_id)) {
$current_state = $workflow
->getTypePlugin()
->getState($moderation_state_id);
if (!$entity
->isSyncing()) {
$update_default_revision = $entity
->isNew() || $current_state
->isDefaultRevisionState() || !$content_moderation_info
->isDefaultRevisionPublished($entity);
$entity
->isDefaultRevision($update_default_revision);
}
$published_state = $current_state
->isPublishedState();
if ($entity instanceof EntityPublishedInterface && $entity
->isPublished() !== $published_state) {
$published_state ? $entity
->setPublished() : $entity
->setUnpublished();
}
}
}
}