Skip to content

Commit

Permalink
[4.x] Prevent updating a term slug from creating two stache terms (#9260
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ryanmitchell authored Jan 8, 2024
1 parent 8b2a371 commit 527325b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Stache/Stores/TaxonomyTermsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ protected function getKeyFromPath($path)

public function save($term)
{
// Since we store terms by slug, if the slug changes it's technically
// a completely new term, and we'll need to delete the existing one.
if (($originalSlug = $term->getOriginal('slug')) && $originalSlug != $term->slug()) {
$existing = Term::find($term->taxonomyHandle().'::'.$originalSlug);
$this->delete($existing->term());
}

// The "old" state shouldn't be maintained within the Stache, otherwise it'll be there
// when the term is retrieved again. Ideally this should be done in a more generic
// location. We'll also use a clone to avoid modifying the original instance.
$term = clone $term;
$term->syncOriginal();

$this->writeItemToDisk($term);

foreach ($term->localizations() as $item) {
Expand All @@ -197,6 +210,21 @@ public function save($term)
}
}

public function delete($term)
{
$this->deleteItemFromDisk($term);

foreach ($term->localizations() as $item) {
$key = $this->getItemKey($item);

$this->forgetItem($key);

$this->forgetPath($key);

$this->resolveIndexes()->filter->isCached()->each->forgetItem($key);
}
}

protected function getItemFromModifiedPath($path)
{
return parent::getItemFromModifiedPath($path)->localizations()->all();
Expand Down

0 comments on commit 527325b

Please sign in to comment.