From 6d0680e3eca80e5f706bc5c158f12cfb335aa578 Mon Sep 17 00:00:00 2001 From: Brandon Kelly Date: Thu, 11 Jul 2019 11:46:19 -0700 Subject: [PATCH] Actually check the database for getHasDescendants() / getTotalDescendants() Fixes #4504 --- CHANGELOG-v3.md | 1 + src/base/Element.php | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 7ffd054d34d..e2893aa7804 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -18,6 +18,7 @@ - Fixed an error that could occur if a template was accessing the deprecated `locale` property of an element query, but `siteId` wasn’t set to an integer. ([#4531](https://github.com/craftcms/cms/issues/4531)) - Fixed a bug where users without the “Publish live changes” permission for a section weren’t able to create new entries. ([#4528](https://github.com/craftcms/cms/issues/4529)) - Fixed a PHP error that could occur when uploading files to Assets fields on the front-end. ([#4382](https://github.com/craftcms/cms/issues/4382)) +- Fixed a bug where elements listed in a Structure view could show descendant toggles even if they had no descendants. ([#4504](https://github.com/craftcms/cms/issues/4504)) ## 3.2.0 - 2019-07-09 diff --git a/src/base/Element.php b/src/base/Element.php index 97252442f6e..ffbe7e3ef3f 100644 --- a/src/base/Element.php +++ b/src/base/Element.php @@ -1632,7 +1632,11 @@ public function getNextSibling() */ public function getHasDescendants(): bool { - return ($this->lft !== null && $this->rgt !== null && $this->rgt > $this->lft + 1); + $descendants = $this->getDescendants(); + if (is_array($descendants)) { + return !empty($descendants); + } + return $descendants->exists(); } /** @@ -1640,11 +1644,11 @@ public function getHasDescendants(): bool */ public function getTotalDescendants(): int { - if ($this->getHasDescendants()) { - return ($this->rgt - $this->lft - 1) / 2; + $descendants = $this->getDescendants(); + if (is_array($descendants)) { + return count($descendants); } - - return 0; + return $descendants->count(); } /**