Skip to content

Commit

Permalink
Smarter saving of Taxonomies, less redundant
Browse files Browse the repository at this point in the history
  • Loading branch information
bobdenotter committed Sep 27, 2023
1 parent 2766f61 commit 9a6f36a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion assets/js/version.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// generated by genversion
export const version = '5.1.99.20';
export const version = '5.1.99.21';
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bolt",
"version": "5.1.99.21",
"version": "5.1.99.23",
"homepage": "https://boltcms.io",
"author": "Bob den Otter <[email protected]> (https://boltcms.io)",
"license": "MIT",
Expand Down
31 changes: 21 additions & 10 deletions src/Controller/Backend/ContentEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,29 +555,40 @@ public function updateField(Field $field, $value, ?string $locale): void
}
}

public function updateTaxonomy(Content $content, string $key, $taxonomy, int $order): void
public function updateTaxonomy(Content $content, string $key, $postedTaxonomy, int $order): void
{
$taxonomy = (new Collection(Json::findArray($taxonomy)))->filter();
$postedTaxonomy = (new Collection(Json::findArray($postedTaxonomy)))->filter();
$contentTaxoSlugs = [];

// Remove old ones
// Remove old ones, if they are not in the current ones
foreach ($content->getTaxonomies($key) as $current) {
$content->removeTaxonomy($current);
// If it's not still present, remove it
if (! in_array($current->getSlug(), $postedTaxonomy->all())) {
$content->removeTaxonomy($current);
}

$contentTaxoSlugs[] = $current->getSlug();
}

// Then (re-) add selected ones
foreach ($taxonomy as $slug) {
$taxonomy = $this->taxonomyRepository->findOneBy([
foreach ($postedTaxonomy as $slug) {
// If we already have it, continue.
if (in_array($slug, $contentTaxoSlugs)) {
continue;
}

$repoTaxonomy = $this->taxonomyRepository->findOneBy([
'type' => $key,
'slug' => $slug,
]);

if ($taxonomy === null) {
$taxonomy = $this->taxonomyRepository->factory($key, (string) $slug);
if ($repoTaxonomy === null) {
$repoTaxonomy = $this->taxonomyRepository->factory($key, (string) $slug);
}

$taxonomy->setSortorder($order);
$repoTaxonomy->setSortorder($order);

$content->addTaxonomy($taxonomy);
$content->addTaxonomy($repoTaxonomy);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class Version
* Stable — 3.0.0
* Development — 3.1.0 alpha 1
*/
public const VERSION = '5.2.0 beta 22';
public const VERSION = '5.2.0 beta 23';
public const CODENAME = '';

/**
Expand Down

0 comments on commit 9a6f36a

Please sign in to comment.