comparison core/modules/content_moderation/src/EntityOperations.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
9 use Drupal\Core\Entity\EntityInterface; 9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Entity\EntityTypeBundleInfoInterface; 10 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
11 use Drupal\Core\Entity\EntityTypeManagerInterface; 11 use Drupal\Core\Entity\EntityTypeManagerInterface;
12 use Drupal\Core\Form\FormBuilderInterface; 12 use Drupal\Core\Form\FormBuilderInterface;
13 use Drupal\content_moderation\Form\EntityModerationForm; 13 use Drupal\content_moderation\Form\EntityModerationForm;
14 use Drupal\Core\Routing\RouteBuilderInterface;
15 use Drupal\workflows\Entity\Workflow;
14 use Symfony\Component\DependencyInjection\ContainerInterface; 16 use Symfony\Component\DependencyInjection\ContainerInterface;
15 17
16 /** 18 /**
17 * Defines a class for reacting to entity events. 19 * Defines a class for reacting to entity events.
18 * 20 *
45 * The entity bundle information service. 47 * The entity bundle information service.
46 * 48 *
47 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface 49 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
48 */ 50 */
49 protected $bundleInfo; 51 protected $bundleInfo;
52
53 /**
54 * The router builder service.
55 *
56 * @var \Drupal\Core\Routing\RouteBuilderInterface
57 */
58 protected $routerBuilder;
50 59
51 /** 60 /**
52 * Constructs a new EntityOperations object. 61 * Constructs a new EntityOperations object.
53 * 62 *
54 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info 63 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
57 * Entity type manager service. 66 * Entity type manager service.
58 * @param \Drupal\Core\Form\FormBuilderInterface $form_builder 67 * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
59 * The form builder. 68 * The form builder.
60 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info 69 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
61 * The entity bundle information service. 70 * The entity bundle information service.
62 */ 71 * @param \Drupal\Core\Routing\RouteBuilderInterface $router_builder
63 public function __construct(ModerationInformationInterface $moderation_info, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, EntityTypeBundleInfoInterface $bundle_info) { 72 * The router builder service.
73 */
74 public function __construct(ModerationInformationInterface $moderation_info, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, EntityTypeBundleInfoInterface $bundle_info, RouteBuilderInterface $router_builder) {
64 $this->moderationInfo = $moderation_info; 75 $this->moderationInfo = $moderation_info;
65 $this->entityTypeManager = $entity_type_manager; 76 $this->entityTypeManager = $entity_type_manager;
66 $this->formBuilder = $form_builder; 77 $this->formBuilder = $form_builder;
67 $this->bundleInfo = $bundle_info; 78 $this->bundleInfo = $bundle_info;
79 $this->routerBuilder = $router_builder;
68 } 80 }
69 81
70 /** 82 /**
71 * {@inheritdoc} 83 * {@inheritdoc}
72 */ 84 */
73 public static function create(ContainerInterface $container) { 85 public static function create(ContainerInterface $container) {
74 return new static( 86 return new static(
75 $container->get('content_moderation.moderation_information'), 87 $container->get('content_moderation.moderation_information'),
76 $container->get('entity_type.manager'), 88 $container->get('entity_type.manager'),
77 $container->get('form_builder'), 89 $container->get('form_builder'),
78 $container->get('entity_type.bundle.info') 90 $container->get('entity_type.bundle.info'),
91 $container->get('router.builder')
79 ); 92 );
80 } 93 }
81 94
82 /** 95 /**
83 * Acts on an entity and set published status based on the moderation state. 96 * Acts on an entity and set published status based on the moderation state.
96 $workflow = $this->moderationInfo->getWorkflowForEntity($entity); 109 $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
97 /** @var \Drupal\content_moderation\ContentModerationState $current_state */ 110 /** @var \Drupal\content_moderation\ContentModerationState $current_state */
98 $current_state = $workflow->getTypePlugin() 111 $current_state = $workflow->getTypePlugin()
99 ->getState($entity->moderation_state->value); 112 ->getState($entity->moderation_state->value);
100 113
101 // This entity is default if it is new, a new translation, the default 114 // This entity is default if it is new, the default revision, or the
102 // revision, or the default revision is not published. 115 // default revision is not published.
103 $update_default_revision = $entity->isNew() 116 $update_default_revision = $entity->isNew()
104 || $entity->isNewTranslation()
105 || $current_state->isDefaultRevisionState() 117 || $current_state->isDefaultRevisionState()
106 || !$this->moderationInfo->isDefaultRevisionPublished($entity); 118 || !$this->moderationInfo->isDefaultRevisionPublished($entity);
107 119
108 // Fire per-entity-type logic for handling the save process. 120 // Fire per-entity-type logic for handling the save process.
109 $this->entityTypeManager 121 $this->entityTypeManager
132 */ 144 */
133 public function entityUpdate(EntityInterface $entity) { 145 public function entityUpdate(EntityInterface $entity) {
134 if ($this->moderationInfo->isModeratedEntity($entity)) { 146 if ($this->moderationInfo->isModeratedEntity($entity)) {
135 $this->updateOrCreateFromEntity($entity); 147 $this->updateOrCreateFromEntity($entity);
136 } 148 }
149 // When updating workflow settings for Content Moderation, we need to
150 // rebuild routes as we may be enabling new entity types and the related
151 // entity forms.
152 elseif ($entity instanceof Workflow && $entity->getTypePlugin()->getPluginId() == 'content_moderation') {
153 $this->routerBuilder->setRebuildNeeded();
154 }
137 } 155 }
138 156
139 /** 157 /**
140 * Creates or updates the moderation state of an entity. 158 * Creates or updates the moderation state of an entity.
141 * 159 *
145 protected function updateOrCreateFromEntity(EntityInterface $entity) { 163 protected function updateOrCreateFromEntity(EntityInterface $entity) {
146 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ 164 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
147 $entity_revision_id = $entity->getRevisionId(); 165 $entity_revision_id = $entity->getRevisionId();
148 $workflow = $this->moderationInfo->getWorkflowForEntity($entity); 166 $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
149 $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity); 167 $content_moderation_state = ContentModerationStateEntity::loadFromModeratedEntity($entity);
168 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
169 $storage = $this->entityTypeManager->getStorage('content_moderation_state');
150 170
151 if (!($content_moderation_state instanceof ContentModerationStateInterface)) { 171 if (!($content_moderation_state instanceof ContentModerationStateInterface)) {
152 $storage = $this->entityTypeManager->getStorage('content_moderation_state');
153 $content_moderation_state = $storage->create([ 172 $content_moderation_state = $storage->create([
154 'content_entity_type_id' => $entity->getEntityTypeId(), 173 'content_entity_type_id' => $entity->getEntityTypeId(),
155 'content_entity_id' => $entity->id(), 174 'content_entity_id' => $entity->id(),
156 // Make sure that the moderation state entity has the same language code 175 // Make sure that the moderation state entity has the same language code
157 // as the moderated entity. 176 // as the moderated entity.
158 'langcode' => $entity->language()->getId(), 177 'langcode' => $entity->language()->getId(),
159 ]); 178 ]);
160 $content_moderation_state->workflow->target_id = $workflow->id(); 179 $content_moderation_state->workflow->target_id = $workflow->id();
161 } 180 }
162 elseif ($content_moderation_state->content_entity_revision_id->value != $entity_revision_id) {
163 // If a new revision of the content has been created, add a new content
164 // moderation state revision.
165 $content_moderation_state->setNewRevision(TRUE);
166 }
167 181
168 // Sync translations. 182 // Sync translations.
169 if ($entity->getEntityType()->hasKey('langcode')) { 183 if ($entity->getEntityType()->hasKey('langcode')) {
170 $entity_langcode = $entity->language()->getId(); 184 $entity_langcode = $entity->language()->getId();
171 if (!$content_moderation_state->hasTranslation($entity_langcode)) { 185 if (!$content_moderation_state->hasTranslation($entity_langcode)) {
172 $content_moderation_state->addTranslation($entity_langcode); 186 $content_moderation_state->addTranslation($entity_langcode);
173 } 187 }
174 if ($content_moderation_state->language()->getId() !== $entity_langcode) { 188 if ($content_moderation_state->language()->getId() !== $entity_langcode) {
175 $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode); 189 $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode);
176 } 190 }
191 }
192
193 // If a new revision of the content has been created, add a new content
194 // moderation state revision.
195 if (!$content_moderation_state->isNew() && $content_moderation_state->content_entity_revision_id->value != $entity_revision_id) {
196 $content_moderation_state = $storage->createRevision($content_moderation_state, $entity->isDefaultRevision());
177 } 197 }
178 198
179 // Create the ContentModerationState entity for the inserted entity. 199 // Create the ContentModerationState entity for the inserted entity.
180 $moderation_state = $entity->moderation_state->value; 200 $moderation_state = $entity->moderation_state->value;
181 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ 201 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
243 * 263 *
244 * @see hook_entity_view() 264 * @see hook_entity_view()
245 * @see EntityFieldManagerInterface::getExtraFields() 265 * @see EntityFieldManagerInterface::getExtraFields()
246 */ 266 */
247 public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) { 267 public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
268 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
248 if (!$this->moderationInfo->isModeratedEntity($entity)) { 269 if (!$this->moderationInfo->isModeratedEntity($entity)) {
249 return; 270 return;
250 } 271 }
251 if (!$this->moderationInfo->isLatestRevision($entity)) { 272 if (isset($entity->in_preview) && $entity->in_preview) {
252 return; 273 return;
253 } 274 }
254 if ($this->moderationInfo->isLiveRevision($entity)) { 275 // If the component is not defined for this display, we have nothing to do.
255 return; 276 if (!$display->getComponent('content_moderation_control')) {
256 } 277 return;
257 // Don't display the moderation form when when: 278 }
258 // - The revision is not translation affected. 279 // The moderation form should be displayed only when viewing the latest
259 // - There are more than one translation languages. 280 // (translation-affecting) revision, unless it was created as published
260 // - The entity has pending revisions. 281 // default revision.
261 if (!$this->moderationInfo->isPendingRevisionAllowed($entity)) { 282 if (!$entity->isLatestRevision() && !$entity->isLatestTranslationAffectedRevision()) {
262 return; 283 return;
263 } 284 }
264 285 if (($entity->isDefaultRevision() || $entity->wasDefaultRevision()) && ($moderation_state = $entity->get('moderation_state')->value)) {
265 $component = $display->getComponent('content_moderation_control'); 286 $workflow = $this->moderationInfo->getWorkflowForEntity($entity);
266 if ($component) { 287 if ($workflow->getTypePlugin()->getState($moderation_state)->isPublishedState()) {
267 $build['content_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity); 288 return;
268 } 289 }
290 }
291
292 $build['content_moderation_control'] = $this->formBuilder->getForm(EntityModerationForm::class, $entity);
269 } 293 }
270 294
271 } 295 }