Skip to content

Commit

Permalink
MINOR: improving find_edit_link_for_object function to work with chil…
Browse files Browse the repository at this point in the history
…d classes of model admin classes.
  • Loading branch information
ShaneW committed Nov 15, 2017
1 parent f4e7525 commit e61008e
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions code/api/CMSEditLinkAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,42 @@ public static function find_edit_link_for_object($objectOrClassName, $action = n
'/admin/security/EditForm/field/Groups/item/'.$objectToEdit->ID.'/edit/'
);
}

if($modelAdminURLOverwrite) {
$classFound = true;
} else {
$cachekey = $modelNameToEdit.'_'.$action;
$cache = SS_Cache::factory('cms_edit_link_cache');
$myAdminClassName = $cache->load($cachekey);
if ($myAdminClassName) {
$myModelAdminclassObject = Injector::inst()->get($myAdminClassName);
if($myModelAdminclassObject instanceof ModelAdmin) {
$classFound = true;
}
}
else {
$classFound = false;
foreach(ClassInfo::subclassesFor('ModelAdmin') as $i => $myAdminClassName) {
if($myAdminClassName == 'ModelAdmin') {continue;}
$classFound = false;
foreach(ClassInfo::subclassesFor('ModelAdmin') as $i => $myAdminClassName) {
for($includeChildren = 0; $includeChildren < 2; $includeChildren++) {

if($myAdminClassName == 'ModelAdmin') {
continue;
}
if(ClassInfo::classImplements($myAdminClassName, 'TestOnly')) {continue;}
$myModelAdminclassObject = Injector::inst()->get($myAdminClassName);
$models = $myModelAdminclassObject->getManagedModels();
foreach($models as $key => $model) {
if($key === $modelNameToEdit || (is_string($model) && $model === $modelNameToEdit)) {
$classFound = true;
$cache->save($myAdminClassName, $cachekey);

break 2;
foreach($models as $model => $modelDetails) {
if(is_string($modelDetails)) {
$model = $modelDetails;
}
$childrenForModelBeingManaged = null;
if($includeChildren) {
$childrenForModelBeingManaged = ClassInfo::subclassesFor($model);
if(is_array($childrenForModelBeingManaged)) {
$modelsToSearch = array_reverse($childrenForModelBeingManaged);
}
} else {
$modelsToSearch = [$model];
}
foreach($modelsToSearch as $modelToSearch) {
if($modelToSearch === $modelNameToEdit) {
if($modelNameToEdit !== $model) {
$modelNameToEdit = $model;
}
$classFound = true;
break 4;
}
}
}
}
Expand Down Expand Up @@ -112,7 +123,7 @@ public static function find_edit_link_for_object($objectOrClassName, $action = n
/**
* Sanitise a model class' name for inclusion in a link
* @param string $className
*
*
* @return string
*/
protected static function sanitize_class_name($className) {
Expand Down

0 comments on commit e61008e

Please sign in to comment.