2

A client wants to be able to get URLs for assets (for, eg. pasting a direct link into an email). For images and PDFs, "View file" works nicely (the file opens in a new browser tab), but for other file types which the browser can't display, they download instead. Is there any way of accessing the full URL of an asset from within the admin panel?

(The best I've been able to suggest is that they reconstruct the URL from the folder structure of their assets, so, eg. "Files > Documents > File.xls" becomes /assets/files/Documents/File.xls, but it would be great if there was a less fiddly way of getting that URL)

2

You could utilize the modifyAssetTableAttributes and getAssetTableAttributeHtml hooks.

So, you would create a plugin, and in the main plugin file you could add something like the following:

public function modifyAssetTableAttributes(&$attributes, $source)
{
    $attributes['filePath'] = Craft::t('File Path');
}

public function getAssetTableAttributeHtml(AssetFileModel $asset, $attribute)
{
    if ($attribute == 'filePath')
    {
        $filePath = $asset->url;
        return $filePath;
    }
}

* Edit *

I did test this, and it works, but because of Craft's ability to reorganize Assets (i.e., drag them to a new folder) it's not just a simple matter of highlighting the link, copying, and pasting.

However, by taking this example a little further, you should be able to add a little JS and have a link or something to that effect output in getAssetTableAttributeHtml that when clicked copies the URL to the user's clipboard. If you go this route, make sure to include the JS in your plugin's init method. Something like:

public function init()
{
    craft()->templates->includeJsResource('yourplugin/your-js.js');
}

Or (hah), for a really basic implementation, you could wrap the string in a URL and just have them right click and "Copy Link Address":

public function getAssetTableAttributeHtml(AssetFileModel $asset, $attribute)
{
    if ($attribute == 'filePath')
    {
        $filePath = '<a href="'.$asset->url.'">' . $asset->url . '</a>';
        return $filePath;
    }
}
| improve this answer | |
1

You could modify the table columns shown on the asset index page and have a column for the URL. Setting this up is only possible with a plugin, but all you need to do is to add modifyAssetTableAttributes and getAssetTableAttributeHtml methods to your plugins primary file and modify the table to your need as described in the docs.

Another hook you could look into with your plugin is the addAssetActions hook.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.