EntityOperations.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace Drupal\content_moderation;
  3. use Drupal\content_moderation\Entity\ContentModerationState as ContentModerationStateEntity;
  4. use Drupal\content_moderation\Entity\ContentModerationStateInterface;
  5. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  6. use Drupal\Core\Entity\ContentEntityInterface;
  7. use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  8. use Drupal\Core\Entity\EntityInterface;
  9. use Drupal\Core\Entity\EntityPublishedInterface;
  10. use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
  11. use Drupal\Core\Entity\EntityTypeManagerInterface;
  12. use Drupal\Core\Form\FormBuilderInterface;
  13. use Drupal\content_moderation\Form\EntityModerationForm;
  14. use Drupal\Core\Routing\RouteBuilderInterface;
  15. use Drupal\workflows\Entity\Workflow;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. /**
  18. * Defines a class for reacting to entity events.
  19. *
  20. * @internal
  21. */
  22. class EntityOperations implements ContainerInjectionInterface {
  23. /**
  24. * The Moderation Information service.
  25. *
  26. * @var \Drupal\content_moderation\ModerationInformationInterface
  27. */
  28. protected $moderationInfo;
  29. /**
  30. * The Entity Type Manager service.
  31. *
  32. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  33. */
  34. protected $entityTypeManager;
  35. /**
  36. * The Form Builder service.
  37. *
  38. * @var \Drupal\Core\Form\FormBuilderInterface
  39. */
  40. protected $formBuilder;
  41. /**
  42. * The entity bundle information service.
  43. *
  44. * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
  45. */
  46. protected $bundleInfo;
  47. /**
  48. * The router builder service.
  49. *
  50. * @var \Drupal\Core\Routing\RouteBuilderInterface
  51. */
  52. protected $routerBuilder;
  53. /**
  54. * Constructs a new EntityOperations object.
  55. *
  56. * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
  57. * Moderation information service.
  58. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  59. * Entity type manager service.
  60. * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
  61. * The form builder.
  62. * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
  63. * The entity bundle information service.
  64. * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
  65. * The router builder service.
  66. */
  67. public function __construct(ModerationInformationInterface $moderation_info, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, EntityTypeBundleInfoInterface $bundle_info, RouteBuilderInterface $router_builder) {
  68. $this->moderationInfo = $moderation_info;
  69. $this->entityTypeManager = $entity_type_manager;
  70. $this->formBuilder = $form_builder;
  71. $this->bundleInfo = $bundle_info;
  72. $this->routerBuilder = $router_builder;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public static function create(ContainerInterface $container) {
  78. return new static(
  79. $container->get('content_moderation.moderation_information'),
  80. $container->get('entity_type.manager'),
  81. $container->get('form_builder'),
  82. $container->get('entity_type.bundle.info'),
  83. $container->get('router.builder')
  84. );
  85. }
  86. /**
  87. * Acts on an entity and set published status based on the moderation state.
  88. *
  89. * @param \Drupal\Core\Entity\EntityInterface $entity
  90. * The entity being saved.
  91. *
  92. * @see hook_entity_presave()
  93. */
  94. public function entityPresave(EntityInterface $entity) {
  95. if (!$this->moderationInfo->isModeratedEntity($entity)) {
  96. return;
  97. }
  98. if ($entity->moderation_state->value) {
  99. $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
  100. /** @var \Drupal\content_moderation\ContentModerationState $current_state */
  101. $current_state = $workflow->getTypePlugin()
  102. ->getState($entity->moderation_state->value);
  103. // This entity is default if it is new, the default revision, or the
  104. // default revision is not published.
  105. $update_default_revision = $entity->isNew()
  106. || $current_state->isDefaultRevisionState()
  107. || !$this->moderationInfo->isDefaultRevisionPublished($entity);
  108. // Fire per-entity-type logic for handling the save process.
  109. $this->entityTypeManager
  110. ->getHandler($entity->getEntityTypeId(), 'moderation')
  111. ->onPresave($entity, $update_default_revision, $current_state->isPublishedState());
  112. }
  113. }
  114. /**
  115. * @param \Drupal\Core\Entity\EntityInterface $entity
  116. * The entity that was just saved.
  117. *
  118. * @see hook_entity_insert()
  119. */
  120. public function entityInsert(EntityInterface $entity) {
  121. if ($this->moderationInfo->isModeratedEntity($entity)) {
  122. $this->updateOrCreateFromEntity($entity);
  123. }
  124. }
  125. /**
  126. * @param \Drupal\Core\Entity\EntityInterface $entity
  127. * The entity that was just saved.
  128. *
  129. * @see hook_entity_update()
  130. */
  131. public function entityUpdate(EntityInterface $entity) {
  132. if ($this->moderationInfo->isModeratedEntity($entity)) {
  133. $this->updateOrCreateFromEntity($entity);
  134. }
  135. // When updating workflow settings for Content Moderation, we need to
  136. // rebuild routes as we may be enabling new entity types and the related
  137. // entity forms.
  138. elseif ($entity instanceof Workflow && $entity->getTypePlugin()->getPluginId() == 'content_moderation') {
  139. $this->routerBuilder->setRebuildNeeded();
  140. }
  141. }
  142. /**
  143. * Creates or updates the moderation state of an entity.
  144. *
  145. * @param \Drupal\Core\Entity\EntityInterface $entity
  146. * The entity to update or create a moderation state for.
  147. */
  148. protected function updateOrCreateFromEntity(EntityInterface $entity) {
  149. /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  150. $entity_revision_id = $entity->getRevisionId();
  151. $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
  152. $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
  153. /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
  154. $storage = $this->entityTypeManager->getStorage('content_moderation_state');
  155. if (!($content_moderation_state instanceof ContentModerationStateInterface)) {
  156. $content_moderation_state = $storage->create([
  157. 'content_entity_type_id' => $entity->getEntityTypeId(),
  158. 'content_entity_id' => $entity->id(),
  159. // Make sure that the moderation state entity has the same language code
  160. // as the moderated entity.
  161. 'langcode' => $entity->language()->getId(),
  162. ]);
  163. $content_moderation_state->workflow->target_id = $workflow->id();
  164. }
  165. // Sync translations.
  166. if ($entity->getEntityType()->hasKey('langcode')) {
  167. $entity_langcode = $entity->language()->getId();
  168. if (!$content_moderation_state->hasTranslation($entity_langcode)) {
  169. $content_moderation_state->addTranslation($entity_langcode);
  170. }
  171. if ($content_moderation_state->language()->getId() !== $entity_langcode) {
  172. $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode);
  173. }
  174. }
  175. // If a new revision of the content has been created, add a new content
  176. // moderation state revision.
  177. if (!$content_moderation_state->isNew() && $content_moderation_state->content_entity_revision_id->value != $entity_revision_id) {
  178. $content_moderation_state = $storage->createRevision($content_moderation_state, $entity->isDefaultRevision());
  179. }
  180. // Create the ContentModerationState entity for the inserted entity.
  181. $moderation_state = $entity->moderation_state->value;
  182. /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  183. if (!$moderation_state) {
  184. $moderation_state = $workflow->getTypePlugin()->getInitialState($entity)->id();
  185. }
  186. $content_moderation_state->set('content_entity_revision_id', $entity_revision_id);
  187. $content_moderation_state->set('moderation_state', $moderation_state);
  188. ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
  189. }
  190. /**
  191. * @param \Drupal\Core\Entity\EntityInterface $entity
  192. * The entity being deleted.
  193. *
  194. * @see hook_entity_delete()
  195. */
  196. public function entityDelete(EntityInterface $entity) {
  197. $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
  198. if ($content_moderation_state) {
  199. $content_moderation_state->delete();
  200. }
  201. }
  202. /**
  203. * @param \Drupal\Core\Entity\EntityInterface $entity
  204. * The entity revision being deleted.
  205. *
  206. * @see hook_entity_revision_delete()
  207. */
  208. public function entityRevisionDelete(EntityInterface $entity) {
  209. /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  210. if (!$entity->isDefaultRevision()) {
  211. $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
  212. if ($content_moderation_state) {
  213. $this->entityTypeManager
  214. ->getStorage('content_moderation_state')
  215. ->deleteRevision($content_moderation_state->getRevisionId());
  216. }
  217. }
  218. }
  219. /**
  220. * @param \Drupal\Core\Entity\EntityInterface $translation
  221. * The entity translation being deleted.
  222. *
  223. * @see hook_entity_translation_delete()
  224. */
  225. public function entityTranslationDelete(EntityInterface $translation) {
  226. /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
  227. if (!$translation->isDefaultTranslation()) {
  228. $langcode = $translation->language()->getId();
  229. $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($translation);
  230. if ($content_moderation_state && $content_moderation_state->hasTranslation($langcode)) {
  231. $content_moderation_state->removeTranslation($langcode);
  232. ContentModerationStateEntity::updateOrCreateFromEntity($content_moderation_state);
  233. }
  234. }
  235. }
  236. /**
  237. * Act on entities being assembled before rendering.
  238. *
  239. * @see hook_entity_view()
  240. * @see EntityFieldManagerInterface::getExtraFields()
  241. */
  242. public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  243. /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  244. if (!$this->moderationInfo->isModeratedEntity($entity)) {
  245. return;
  246. }
  247. if (isset($entity->in_preview) && $entity->in_preview) {
  248. return;
  249. }
  250. // If the component is not defined for this display, we have nothing to do.
  251. if (!$display->getComponent('content_moderation_control')) {
  252. return;
  253. }
  254. // The moderation form should be displayed only when viewing the latest
  255. // (translation-affecting) revision, unless it was created as published
  256. // default revision.
  257. if (($entity->isDefaultRevision() || $entity->wasDefaultRevision()) && $this->isPublished($entity)) {
  258. return;
  259. }
  260. if (!$entity->isLatestRevision() && !$entity->isLatestTranslationAffectedRevision()) {
  261. return;
  262. }
  263. $build['content_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity);
  264. }
  265. /**
  266. * Checks if the entity is published.
  267. *
  268. * This method is optimized to not have to unnecessarily load the moderation
  269. * state and workflow if it is not required.
  270. *
  271. * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  272. * The entity to check.
  273. *
  274. * @return bool
  275. * TRUE if the entity is published, FALSE otherwise.
  276. */
  277. protected function isPublished(ContentEntityInterface $entity) {
  278. // If the entity implements EntityPublishedInterface directly, check that
  279. // first, otherwise fall back to check through the workflow state.
  280. if ($entity instanceof EntityPublishedInterface) {
  281. return $entity->isPublished();
  282. }
  283. if ($moderation_state = $entity->get('moderation_state')->value) {
  284. $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
  285. return $workflow->getTypePlugin()->getState($moderation_state)->isPublishedState();
  286. }
  287. return FALSE;
  288. }
  289. }