Skip to content

Commit

Permalink
MINOR: make use of getParent method to make faster
Browse files Browse the repository at this point in the history
This change will make Silverstripe much faster (i hope), because it uses SiteTree::getParent() instead of the magic / custom method Parent()
  • Loading branch information
sunnysideup authored and GuySartorelli committed Dec 20, 2023
1 parent d5faa01 commit e1da194
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions code/Model/SiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ public function getMimeType()
public function RelativeLink($action = null)
{
if ($this->ParentID && self::config()->get('nested_urls')) {
$parent = $this->Parent();
$parent = $this->getParent();
// If page is removed select parent from version history (for archive page view)
if ((!$parent || !$parent->exists()) && !$this->isOnDraft()) {
$parent = Versioned::get_latest_version(self::class, $this->ParentID);
Expand Down Expand Up @@ -809,7 +809,7 @@ public function isOrphaned()
}

// Parent must exist and not be an orphan itself
$parent = $this->Parent();
$parent = $this->getParent();
return !$parent || !$parent->exists() || $parent->isOrphaned();
}

Expand Down Expand Up @@ -863,7 +863,7 @@ public function InSection($sectionName)
if ($sectionName === $page->URLSegment) {
return true;
}
$page = $page->Parent();
$page = $page->getParent();
}
return false;
}
Expand Down Expand Up @@ -966,7 +966,7 @@ public function getBreadcrumbItems($maxDepth = 20, $stopAtPageType = false, $sho
$pages[] = $page;
}

$page = $page->Parent();
$page = $page->getParent();
}

return new ArrayList(array_reverse($pages ?? []));
Expand Down Expand Up @@ -995,13 +995,13 @@ public function setParent($item)
/**
* Get the parent of this page.
*
* @return SiteTree Parent of this page
* @return SiteTree|null
*/
public function getParent()
{
$parentID = $this->getField("ParentID");
if ($parentID) {
return SiteTree::get_by_id(self::class, $parentID);
return SiteTree::get_by_id($parentID);
}
return null;
}
Expand Down Expand Up @@ -1176,7 +1176,7 @@ public function canView($member = null)
// check for inherit
if ($this->CanViewType === InheritedPermissions::INHERIT) {
if ($this->ParentID) {
return $this->Parent()->canView($member);
return $this->getParent()->canView($member);
} else {
return $this->getSiteConfig()->canViewPages($member);
}
Expand Down Expand Up @@ -1872,7 +1872,7 @@ public function validURLSegment()
// Check known urlsegment blacklists
if (self::config()->get('nested_urls') && $this->ParentID) {
// Guard against url segments for sub-pages
$parent = $this->Parent();
$parent = $this->getParent();
if ($controller = ModelAsController::controller_for($parent)) {
if ($controller instanceof Controller && $controller->hasAction($this->URLSegment)) {
return false;
Expand Down Expand Up @@ -2132,7 +2132,7 @@ public function getCMSFields()

$baseLink = Controller::join_links(
Director::absoluteBaseURL(),
(self::config()->get('nested_urls') && $this->ParentID ? $this->Parent()->RelativeLink(true) : null)
(self::config()->get('nested_urls') && $this->ParentID ? $this->getParent()->RelativeLink(true) : null)
);

$urlsegment = SiteTreeURLSegmentField::create("URLSegment", $this->fieldLabel('URLSegment'))
Expand Down Expand Up @@ -2755,7 +2755,7 @@ protected function getClassDropdown()
if ($instance instanceof HiddenClass) {
continue;
}
if (!$instance->canCreate(null, ['Parent' => $this->ParentID ? $this->Parent() : null])) {
if (!$instance->canCreate(null, ['Parent' => $this->ParentID ? $this->getParent() : null])) {
continue;
}
}
Expand Down Expand Up @@ -3033,7 +3033,7 @@ public function Level($level)
{
$parent = $this;
$stack = [$parent];
while (($parent = $parent->Parent()) && $parent->exists()) {
while (($parent = $parent->getParent()) && $parent->exists()) {
array_unshift($stack, $parent);
}

Expand All @@ -3048,7 +3048,7 @@ public function Level($level)
public function getPageLevel()
{
if ($this->ParentID) {
return 1 + $this->Parent()->getPageLevel();
return 1 + $this->getParent()->getPageLevel();
}
return 1;
}
Expand Down

0 comments on commit e1da194

Please sign in to comment.