Skip to content

Commit

Permalink
Revisions part 2
Browse files Browse the repository at this point in the history
This reverts commit d0fe364.
  • Loading branch information
brandonkelly committed Feb 6, 2024
1 parent d0fe364 commit 08be879
Showing 1 changed file with 29 additions and 61 deletions.
90 changes: 29 additions & 61 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ public function getEagerLoadingMap(array $sourceElements): array|null|false
public function afterElementPropagate(ElementInterface $element, bool $isNew): void
{
$this->entryManager()->maintainNestedElements($element, $isNew);
$this->_handleCreatedRevision($element);
parent::afterElementPropagate($element, $isNew);
}

Expand All @@ -573,6 +572,21 @@ public function afterDuplicateNestedElements(DuplicateNestedElementsEvent $event
$this->_adjustFieldValue($event->target, $oldEntryIds, $newElementIds, true);
}

public function afterCreateRevisions(DuplicateNestedElementsEvent $event): void
{
$revisionOwners = [
$event->target,
...$event->target->getLocalized()->status(null)->all(),
];

$oldElementIds = array_keys($event->newElementIds);
$newElementIds = array_values($event->newElementIds);

foreach ($revisionOwners as $revisionOwner) {
$this->_adjustFieldValue($revisionOwner, $oldElementIds, $newElementIds, false);
}
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -862,7 +876,7 @@ protected function prepValueForInput($value, ?ElementInterface $element, bool $s
// (https://github.com/craftcms/ckeditor/issues/96)
$value = $this->_normalizeFigures($value);

$value = $this->_prepNestedEntriesForDisplay($value, $element, $static);
$value = $this->_prepNestedEntriesForDisplay($value, $element->siteId, $static);
}

return parent::prepValueForInput($value, $element);
Expand Down Expand Up @@ -900,6 +914,10 @@ private function entryManager(): NestedElementManager
NestedElementManager::EVENT_AFTER_DUPLICATE_NESTED_ELEMENTS,
[$this, 'afterDuplicateNestedElements'],
);
$this->_entryManager->on(
NestedElementManager::EVENT_AFTER_CREATE_REVISIONS,
[$this, 'afterCreateRevisions'],
);
}

return $this->_entryManager;
Expand Down Expand Up @@ -1022,52 +1040,6 @@ private function createEntryQuery(?ElementInterface $owner): EntryQuery
return $query;
}

/**
* If a revision had been created, it ensures the markers in the cke field of a revision match the revision entries.
*
* @param ElementInterface $element
* @return void
*/
private function _handleCreatedRevision(ElementInterface $element): void
{
if ($element->duplicateOf !== null && $element->getIsRevision()) {
$revisionOwners = [$element] + $element::find()
->id($element->id ?: false)
->siteId(['not', $element->siteId])
->drafts($element->getIsDraft())
->provisionalDrafts($element->isProvisionalDraft)
->revisions($element->getIsRevision())
->status(null)
->ignorePlaceholders()
->indexBy('siteId')
->all();

foreach ($revisionOwners as $revisionOwner) {
// get elementIds and their canonicalIds for the revision element
$elements = (new Query())
->select(['elementId' => 'entries.id', 'canonicalId' => 'elements.canonicalId'])
->from(['entries' => Table::ENTRIES])
->innerJoin(['elements' => Table::ELEMENTS], '[[elements.id]] = [[entries.id]]')
->innerJoin(['elements_owners' => Table::ELEMENTS_OWNERS], '[[elements_owners.elementId]] = [[entries.id]]')
->innerJoin(['elements_sites' => Table::ELEMENTS_SITES], '[[elements_sites.elementId]] = [[entries.id]]')
->where([
'entries.fieldId' => $this->id,
'elements_owners.ownerId' => $revisionOwner->id,
'elements_sites.siteId' => $revisionOwner->siteId,
])->all();

// get all elementIds for the owner revision
$newElementIds = array_map(fn($item) => $item['elementId'], $elements);

// and get their canonicals
$oldElementIds = array_map(fn($item) => $item['canonicalId'], $elements);

// update canonicals to revision's elementIds
$this->_adjustFieldValue($revisionOwner, $oldElementIds, $newElementIds, false);
}
}
}

/**
* Returns entry type options in form of an array with 'label' and 'value' keys for each option.
*
Expand Down Expand Up @@ -1096,11 +1068,11 @@ private function _getEntryTypeOptions(): array
* If it's a static request
*
* @param string $value
* @param ElementInterface $element
* @param int $elementSiteId
* @param bool $static
* @return string
*/
private function _prepNestedEntriesForDisplay(string $value, ElementInterface $element, bool $static = false): string
private function _prepNestedEntriesForDisplay(string $value, int $elementSiteId, bool $static = false): string
{
[$entryIds, $markers] = $this->findEntries($value, true);

Expand All @@ -1109,19 +1081,15 @@ private function _prepNestedEntriesForDisplay(string $value, ElementInterface $e
}

$isCpRequest = Craft::$app->getRequest()->getIsCpRequest();
$query = Entry::find()
$entries = Entry::find()
->id($entryIds)
->siteId($element->siteId)
->siteId($elementSiteId)
->status(null)
->indexBy('id');

if ($element->getIsRevision()) {
$query
->revisions(null)
->trashed(null);
}

$entries = $query->all();
->drafts(null)
->revisions(null)
->trashed(null)
->indexBy('id')
->all();

foreach ($markers as $i => $marker) {
$entryId = $entryIds[$i];
Expand Down

0 comments on commit 08be879

Please sign in to comment.