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