From 90f498296c49fb200f62eb986193b115b5f4fba6 Mon Sep 17 00:00:00 2001 From: Nicolaas Date: Wed, 8 Jul 2020 23:38:38 +1200 Subject: [PATCH] Update CMSEditLinkAPI.php --- src/Api/CMSEditLinkAPI.php | 57 +++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/Api/CMSEditLinkAPI.php b/src/Api/CMSEditLinkAPI.php index 1fca388..898dab7 100644 --- a/src/Api/CMSEditLinkAPI.php +++ b/src/Api/CMSEditLinkAPI.php @@ -64,9 +64,43 @@ public static function find_edit_link_for_object($objectOrClassName, $action = n ); } + $classFound = false; if ($modelAdminURLOverwrite) { $classFound = true; } else { + $modelNameToEdit = self::getModelAdmin($modelNameToEdit); + if($modelNameToEdit) { + $classFound = true; + } + } + if ($classFound) { + $modelNameToEdit = self::sanitize_class_name($modelNameToEdit); + if ($id === 0) { + $id = 'new'; + } + if (! $action) { + $action = $modelNameToEdit . '/EditForm/field/' . $modelNameToEdit . '/item/' . $id . '/'; + } + if ($modelAdminURLOverwrite) { + $link = '/admin/' . $modelAdminURLOverwrite . '/' . $action; + } else { + $link = $myModelAdminclassObject->Link($action); + } + + return Controller::join_links( + Director::baseURL(), + $link + ); + } + + return ''; + } + + protected static function getModelAdmin($modelNameToEdit) : string + { + $originalModelNameToEdit = $modelNameToEdit; + if(! isset(self::$_cache[$originalModelNameToEdit])) { + self::$_cache[$originalModelNameToEdit] = ''; $classFound = false; foreach (ClassInfo::subclassesFor(ModelAdmin::class) as $i => $myAdminClassName) { for ($includeChildren = 0; $includeChildren < 2; $includeChildren++) { @@ -105,27 +139,10 @@ public static function find_edit_link_for_object($objectOrClassName, $action = n } } } - if ($classFound) { - $modelNameToEdit = self::sanitize_class_name($modelNameToEdit); - if ($id === 0) { - $id = 'new'; - } - if (! $action) { - $action = $modelNameToEdit . '/EditForm/field/' . $modelNameToEdit . '/item/' . $id . '/'; - } - if ($modelAdminURLOverwrite) { - $link = '/admin/' . $modelAdminURLOverwrite . '/' . $action; - } else { - $link = $myModelAdminclassObject->Link($action); - } - - return Controller::join_links( - Director::baseURL(), - $link - ); + if($classFound) { + self::$_cache[$originalModelNameToEdit] = $modelNameToEdit; } - - return ''; + return self::$_cache[$originalModelNameToEdit]; } protected static function sanitize_class_name(string $className) : string