1

I'm writing a custom module that's going to need to retrieve channel data using a direct database query.

One of the custom fields is an Assets image and I need to get the full image URL and alt_text in the most efficient way possible.

Is there a "best practice" way to get all this data out of Assets when armed with an entry ID and custom field ID? In pseudo-code I'd quite like to do this:

$assets = new Assets;
$file = $assets->getFileData($entry_id,$field_id);

$url = $file->url;
$alt_text = $file->alt_text;

Alternatively I could JOIN the necessary tables in my query but it looks like there would be quite a few.

2

The quick way would be to use an SQL query (something like http://pastie.org/private/x6uue9y6ieqvlqf6ai59q), but that would not get you the URL, though, as that depends on several other settings. If you're confident that the settings for the upload folder (or Assets external source) for that will never change, this will probably not be an issue for you.

The neat way would be to use the Assets API with something like this: http://pastie.org/private/bzcqi92f3wqy0htqm3w. That would return you an instance of Assets_base_file.

You would still have to use a query to get the file_id from exp_assets_selections, though.

I'll add the "get_file_by_entry_and_field" method to the feature list for the next release of Assets, but I don't have any estimates on when will that arrive, though..

  • Thanks for all that. I think get_file_by_id is probably what I was looking for, but I ended up going the "massive query" route. That way I get all the selections and folder info along with my main channel query. I'll keep an eye out for get_file_by_entry_and_field in a future release. – Darren Nov 12 '13 at 6:51
1

I would load Assets library and use it. Example from one of my addons:

$this->EE->load->add_package_path(PATH_THIRD.'assets/');
$this->EE->load->library('assets_lib');
$src = $this->EE->assets_lib->get_file_url($file_id);
$this->EE->load->remove_package_path(PATH_THIRD.'assets/');

Unfortunately Assets don't really have developer's documentation, but function names mostly speak for themselves.

  • Yuri, yeah, we added documentation for hooks and config settings with the last release - documenting Assets API is up next :) – Andris Sevcenko Nov 12 '13 at 10:14

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.