DataObject with asset (image) // used on // How to get the link?

Silverstripe Version: 4.9

Question:

I have created a custom DataObject “artist” which has an asset (image) field/relation.

In asset admin, on the images detail page, I can find the image and on the “used on” panel the related “artist”-DataObject shows up as expected.

But when I click I am redirected to the homepage (/#) not to the objects edit url.

How can I get the proper url?
I guess I have to implement a certain method() but have not figured out yet which one.

I am grateful for a little hint …

Thank you

Ok, I just found it myself :slight_smile:

CMSEditLink()

https://api.silverstripe.org/4/SilverStripe/Assets/File.html#method_CMSEditLink

To get the proper URL for a related object in the “Used on” panel, you can use the Link() method of the object. This will return the URL for the object’s “view” page, which you can then use to redirect the user.

For example, if your artist object has a Link() method that returns the correct URL for the object’s view page, you can use the following code to redirect the user to the object’s edit page:

$artist = Artist::get()->byID(123);
$editURL = $artist->Link() . '/EditForm';

You can then use this $editURL variable to redirect the user to the object’s edit page.

Alternatively, you can use the CMSEditLink() method provided by the CMSPageEditController class to get the edit URL for a page or data object. This method requires the ID of the object as a parameter, and returns the edit URL for the object.

For example:

$artist = Artist::get()->byID(123);
$editURL = CMSPageEditController::singleton()->CMSEditLink($artist->ID);
1 Like