EntityOperations.php in Drupal 8
File
core/modules/content_moderation/src/EntityOperations.php
View source
<?php
namespace Drupal\content_moderation;
use Drupal\content_moderation\Entity\ContentModerationState as ContentModerationStateEntity;
use Drupal\content_moderation\Entity\ContentModerationStateInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\content_moderation\Form\EntityModerationForm;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\workflows\Entity\Workflow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityOperations implements ContainerInjectionInterface {
protected $moderationInfo;
protected $entityTypeManager;
protected $formBuilder;
protected $bundleInfo;
protected $routerBuilder;
public function __construct(ModerationInformationInterface $moderation_info, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, EntityTypeBundleInfoInterface $bundle_info, RouteBuilderInterface $router_builder) {
$this->moderationInfo = $moderation_info;
$this->entityTypeManager = $entity_type_manager;
$this->formBuilder = $form_builder;
$this->bundleInfo = $bundle_info;
$this->routerBuilder = $router_builder;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('content_moderation.moderation_information'), $container
->get('entity_type.manager'), $container
->get('form_builder'), $container
->get('entity_type.bundle.info'), $container
->get('router.builder'));
}
public function entityPresave(EntityInterface $entity) {
if (!$this->moderationInfo
->isModeratedEntity($entity)) {
return;
}
if ($entity->moderation_state->value) {
$workflow = $this->moderationInfo
->getWorkflowForEntity($entity);
$current_state = $workflow
->getTypePlugin()
->getState($entity->moderation_state->value);
$update_default_revision = $entity
->isNew() || $current_state
->isDefaultRevisionState() || !$this->moderationInfo
->isDefaultRevisionPublished($entity);
$this->entityTypeManager
->getHandler($entity
->getEntityTypeId(), 'moderation')
->onPresave($entity, $update_default_revision, $current_state
->isPublishedState());
}
}
public function entityInsert(EntityInterface $entity) {
if ($this->moderationInfo
->isModeratedEntity($entity)) {
$this
->updateOrCreateFromEntity($entity);
}
}
public function entityUpdate(EntityInterface $entity) {
if ($this->moderationInfo
->isModeratedEntity($entity)) {
$this
->updateOrCreateFromEntity($entity);
}
elseif ($entity instanceof Workflow && $entity
->getTypePlugin()
->getPluginId() == 'content_moderation') {
$this->routerBuilder
->setRebuildNeeded();
}
}
protected function updateOrCreateFromEntity(EntityInterface $entity) {
$entity_revision_id = $entity
->getRevisionId();
$workflow = $this->moderationInfo
->getWorkflowForEntity($entity);
$content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
$storage = $this->entityTypeManager
->getStorage('content_moderation_state');
if (!$content_moderation_state instanceof ContentModerationStateInterface) {
$content_moderation_state = $storage
->create([
'content_entity_type_id' => $entity
->getEntityTypeId(),
'content_entity_id' => $entity
->id(),
'langcode' => $entity
->language()
->getId(),
]);
$content_moderation_state->workflow->target_id = $workflow
->id();
}
if ($entity
->getEntityType()
->hasKey('langcode')) {
$entity_langcode = $entity
->language()
->getId();
if ($entity
->isDefaultTranslation()) {
$content_moderation_state->langcode = $entity_langcode;
}
else {
if (!$content_moderation_state
->hasTranslation($entity_langcode)) {
$content_moderation_state
->addTranslation($entity_langcode);
}
if ($content_moderation_state
->language()
->getId() !== $entity_langcode) {
$content_moderation_state = $content_moderation_state
->getTranslation($entity_langcode);
}
}
}
if (!$content_moderation_state
->isNew() && $content_moderation_state->content_entity_revision_id->value != $entity_revision_id) {
$content_moderation_state = $storage
->createRevision($content_moderation_state, $entity
->isDefaultRevision());
}
$moderation_state = $entity->moderation_state->value;
if (!$moderation_state) {
$moderation_state = $workflow
->getTypePlugin()
->getInitialState($entity)
->id();
}
$content_moderation_state
->set('content_entity_revision_id', $entity_revision_id);
$content_moderation_state
->set('moderation_state', $moderation_state);
ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
}
public function entityDelete(EntityInterface $entity) {
$content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
if ($content_moderation_state) {
$content_moderation_state
->delete();
}
}
public function entityRevisionDelete(EntityInterface $entity) {
if ($content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity)) {
if ($content_moderation_state
->isDefaultRevision()) {
$content_moderation_state
->delete();
}
else {
$this->entityTypeManager
->getStorage('content_moderation_state')
->deleteRevision($content_moderation_state
->getRevisionId());
}
}
}
public function entityTranslationDelete(EntityInterface $translation) {
if (!$translation
->isDefaultTranslation()) {
$langcode = $translation
->language()
->getId();
$content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($translation);
if ($content_moderation_state && $content_moderation_state
->hasTranslation($langcode)) {
$content_moderation_state
->removeTranslation($langcode);
ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
}
}
}
public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if (!$this->moderationInfo
->isModeratedEntity($entity)) {
return;
}
if (isset($entity->in_preview) && $entity->in_preview) {
return;
}
if (!$display
->getComponent('content_moderation_control')) {
return;
}
if (($entity
->isDefaultRevision() || $entity
->wasDefaultRevision()) && $this
->isPublished($entity)) {
return;
}
if (!$entity
->isLatestRevision() && !$entity
->isLatestTranslationAffectedRevision()) {
return;
}
$build['content_moderation_control'] = $this->formBuilder
->getForm(EntityModerationForm::class, $entity);
}
protected function isPublished(ContentEntityInterface $entity) {
if ($entity instanceof EntityPublishedInterface) {
return $entity
->isPublished();
}
if ($moderation_state = $entity
->get('moderation_state')->value) {
$workflow = $this->moderationInfo
->getWorkflowForEntity($entity);
return $workflow
->getTypePlugin()
->getState($moderation_state)
->isPublishedState();
}
return FALSE;
}
}