Skip to content

Commit

Permalink
Update CMSEditLinkAPI.php
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnysideup authored Jul 8, 2020
1 parent 3cc3cad commit 90f4982
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions src/Api/CMSEditLinkAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 90f4982

Please sign in to comment.