From 6a79668a2aaad0d2f14719026697cf74948c8255 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 9 Dec 2024 12:53:47 +1300 Subject: [PATCH] ENH Update code to avoid calling deprecated code --- .../ShareDraftContentControllerExtension.php | 23 +++++++++++++++---- .../Includes/LeftAndMain_ViewModeSelector.ss | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Extensions/ShareDraftContentControllerExtension.php b/src/Extensions/ShareDraftContentControllerExtension.php index c205f37..9468e18 100644 --- a/src/Extensions/ShareDraftContentControllerExtension.php +++ b/src/Extensions/ShareDraftContentControllerExtension.php @@ -4,6 +4,7 @@ use SilverStripe\Control\Controller; use SilverStripe\Core\Extension; +use SilverStripe\Security\Member; use SilverStripe\Security\Security; /** @@ -24,17 +25,29 @@ class ShareDraftContentControllerExtension extends Extension public function MakeShareDraftLink() { if ($member = Security::getCurrentUser()) { - if ($this->owner->hasMethod('CurrentPage') && $this->owner->CurrentPage()->canView($member)) { - return $this->owner->CurrentPage()->ShareTokenLink(); - } - if ($this->owner->hasMethod('canView') && $this->owner->canView($member)) { - return $this->owner->ShareTokenLink(); + if ($this->owner->hasMethod('currentRecord')) { + $link = $this->getShareTokenLink($this->owner->currentRecord(), $member); + } elseif ($this->owner->hasMethod('CurrentPage')) { + // Could be a non-LeftAndMain controller, since the extension is applied directly to Controller + $link = $this->getShareTokenLink($this->owner->CurrentPage(), $member); } + $link ??= $this->getShareTokenLink($this->owner, $member); + } + if ($link) { + return $link; } return Security::permissionFailure(); } + private function getShareTokenLink(object $record, Member $member): ?string + { + if ($record->hasMethod('canView') && $record->canView($member)) { + return $record->ShareTokenLink(); + } + return null; + } + /** * @return string */ diff --git a/templates/SilverStripe/Admin/Includes/LeftAndMain_ViewModeSelector.ss b/templates/SilverStripe/Admin/Includes/LeftAndMain_ViewModeSelector.ss index 625da5a..6a47657 100644 --- a/templates/SilverStripe/Admin/Includes/LeftAndMain_ViewModeSelector.ss +++ b/templates/SilverStripe/Admin/Includes/LeftAndMain_ViewModeSelector.ss @@ -2,7 +2,7 @@ <%-- Placeholder is re-rendered by the ShareDraftContent React component --%>