function EntityOperations::updateOrCreateFromEntity
Same name in other branches
- 9 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::updateOrCreateFromEntity()
- 8.9.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::updateOrCreateFromEntity()
- 11.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations::updateOrCreateFromEntity()
Creates or updates the moderation state of an entity.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The entity to update or create a moderation state for.
2 calls to EntityOperations::updateOrCreateFromEntity()
- EntityOperations::entityInsert in core/
modules/ content_moderation/ src/ EntityOperations.php - EntityOperations::entityUpdate in core/
modules/ content_moderation/ src/ EntityOperations.php
File
-
core/
modules/ content_moderation/ src/ EntityOperations.php, line 165
Class
- EntityOperations
- Defines a class for reacting to entity events.
Namespace
Drupal\content_moderationCode
protected function updateOrCreateFromEntity(EntityInterface $entity) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity_revision_id = $entity->getRevisionId();
$workflow = $this->moderationInfo
->getWorkflowForEntity($entity);
$content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
/** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
$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(),
// Make sure that the moderation state entity has the same language code
// as the moderated entity.
'langcode' => $entity->language()
->getId(),
]);
$content_moderation_state->workflow->target_id = $workflow->id();
}
// Sync translations.
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 a new revision of the content has been created, add a new content
// moderation state revision.
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());
}
// Create the ContentModerationState entity for the inserted entity.
$moderation_state = $entity->moderation_state->value;
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
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);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.