comparison core/modules/workspaces/src/EntityOperations.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
55 $container->get('workspaces.manager') 55 $container->get('workspaces.manager')
56 ); 56 );
57 } 57 }
58 58
59 /** 59 /**
60 * Acts on entities when loaded. 60 * Acts on entity IDs before they are loaded.
61 * 61 *
62 * @see hook_entity_load() 62 * @see hook_entity_preload()
63 */ 63 */
64 public function entityLoad(array &$entities, $entity_type_id) { 64 public function entityPreload(array $ids, $entity_type_id) {
65 $entities = [];
66
65 // Only run if the entity type can belong to a workspace and we are in a 67 // Only run if the entity type can belong to a workspace and we are in a
66 // non-default workspace. 68 // non-default workspace.
67 if (!$this->workspaceManager->shouldAlterOperations($this->entityTypeManager->getDefinition($entity_type_id))) { 69 if (!$this->workspaceManager->shouldAlterOperations($this->entityTypeManager->getDefinition($entity_type_id))) {
68 return; 70 return $entities;
69 } 71 }
70 72
71 // Get a list of revision IDs for entities that have a revision set for the 73 // Get a list of revision IDs for entities that have a revision set for the
72 // current active workspace. If an entity has multiple revisions set for a 74 // current active workspace. If an entity has multiple revisions set for a
73 // workspace, only the one with the highest ID is returned. 75 // workspace, only the one with the highest ID is returned.
74 $entity_ids = array_keys($entities);
75 $max_revision_id = 'max_target_entity_revision_id'; 76 $max_revision_id = 'max_target_entity_revision_id';
76 $results = $this->entityTypeManager 77 $query = $this->entityTypeManager
77 ->getStorage('workspace_association') 78 ->getStorage('workspace_association')
78 ->getAggregateQuery() 79 ->getAggregateQuery()
79 ->accessCheck(FALSE) 80 ->accessCheck(FALSE)
80 ->allRevisions() 81 ->allRevisions()
81 ->aggregate('target_entity_revision_id', 'MAX', NULL, $max_revision_id) 82 ->aggregate('target_entity_revision_id', 'MAX', NULL, $max_revision_id)
82 ->groupBy('target_entity_id') 83 ->groupBy('target_entity_id')
83 ->condition('target_entity_type_id', $entity_type_id) 84 ->condition('target_entity_type_id', $entity_type_id)
84 ->condition('target_entity_id', $entity_ids, 'IN') 85 ->condition('workspace', $this->workspaceManager->getActiveWorkspace()->id());
85 ->condition('workspace', $this->workspaceManager->getActiveWorkspace()->id()) 86
86 ->execute(); 87 if ($ids) {
87 88 $query->condition('target_entity_id', $ids, 'IN');
88 // Since hook_entity_load() is called on both regular entity load as well as 89 }
89 // entity revision load, we need to prevent infinite recursion by checking 90
90 // whether the default revisions were already swapped with the workspace 91 $results = $query->execute();
91 // revision.
92 // @todo This recursion protection should be removed when
93 // https://www.drupal.org/project/drupal/issues/2928888 is resolved.
94 if ($results) {
95 $results = array_filter($results, function ($result) use ($entities, $max_revision_id) {
96 return $entities[$result['target_entity_id']]->getRevisionId() != $result[$max_revision_id];
97 });
98 }
99 92
100 if ($results) { 93 if ($results) {
101 /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */ 94 /** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
102 $storage = $this->entityTypeManager->getStorage($entity_type_id); 95 $storage = $this->entityTypeManager->getStorage($entity_type_id);
103 96
106 $swap_revision_ids = array_column($results, $max_revision_id); 99 $swap_revision_ids = array_column($results, $max_revision_id);
107 foreach ($storage->loadMultipleRevisions($swap_revision_ids) as $revision) { 100 foreach ($storage->loadMultipleRevisions($swap_revision_ids) as $revision) {
108 $entities[$revision->id()] = $revision; 101 $entities[$revision->id()] = $revision;
109 } 102 }
110 } 103 }
104
105 return $entities;
111 } 106 }
112 107
113 /** 108 /**
114 * Acts on an entity before it is created or updated. 109 * Acts on an entity before it is created or updated.
115 * 110 *
131 // default workspace. 126 // default workspace.
132 if (!$this->workspaceManager->isEntityTypeSupported($entity_type)) { 127 if (!$this->workspaceManager->isEntityTypeSupported($entity_type)) {
133 throw new \RuntimeException('This entity can only be saved in the default workspace.'); 128 throw new \RuntimeException('This entity can only be saved in the default workspace.');
134 } 129 }
135 130
136 /** @var \Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */ 131 /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */
137 if (!$entity->isNew() && !isset($entity->_isReplicating)) { 132 if (!$entity->isNew() && !$entity->isSyncing()) {
138 // Force a new revision if the entity is not replicating. 133 // Force a new revision if the entity is not replicating.
139 $entity->setNewRevision(TRUE); 134 $entity->setNewRevision(TRUE);
140 135
141 // All entities in the non-default workspace are pending revisions, 136 // All entities in the non-default workspace are pending revisions,
142 // regardless of their publishing status. This means that when creating 137 // regardless of their publishing status. This means that when creating
248 * If the passed-in entity can belong to a workspace and already has a 243 * If the passed-in entity can belong to a workspace and already has a
249 * WorkspaceAssociation entity, then a new revision of this will be created with 244 * WorkspaceAssociation entity, then a new revision of this will be created with
250 * the new information. Otherwise, a new WorkspaceAssociation entity is created to 245 * the new information. Otherwise, a new WorkspaceAssociation entity is created to
251 * store the passed-in entity's information. 246 * store the passed-in entity's information.
252 * 247 *
253 * @param \Drupal\Core\Entity\EntityInterface $entity 248 * @param \Drupal\Core\Entity\RevisionableInterface $entity
254 * The entity to update or create from. 249 * The entity to update or create from.
255 */ 250 */
256 protected function trackEntity(EntityInterface $entity) { 251 protected function trackEntity(RevisionableInterface $entity) {
257 /** @var \Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */
258 // If the entity is not new, check if there's an existing 252 // If the entity is not new, check if there's an existing
259 // WorkspaceAssociation entity for it. 253 // WorkspaceAssociation entity for it.
260 $workspace_association_storage = $this->entityTypeManager->getStorage('workspace_association'); 254 $workspace_association_storage = $this->entityTypeManager->getStorage('workspace_association');
261 if (!$entity->isNew()) { 255 if (!$entity->isNew()) {
262 $workspace_associations = $workspace_association_storage->loadByProperties([ 256 $workspace_associations = $workspace_association_storage->loadByProperties([