Hi folks,
Through a plugin I’m writing, I need to replace the {filedir_x} tag returned from a database record. This is what I’ve got so far …
function _getFilePath( $data ) {
global $FNS;
return str_replace( 'https://ellislab.com/asset/images/team-photo/', $FNS->file_paths[1], $data );
}
I’m not comfortable with accessing the $FNS variables directly like that. Is there an actual function provided by EE through the API to do this?
Ideally, I need to replace all types of {filedir_x} with whatever the value is in the file upload preferences. So, if the database returns “https://ellislab.com/asset/images/ent-partner-logo/filename.pdf”, I would like to pass that string into the function and have it return the appropriate path.
Thanks, Af.
Yes the typograpy class will certainly parse them for you, but there isn’t a direct API call to return the actual paths. What is wrong with directly accessing the $FNS variables?
Other than what you’ve already got you could do something like:
$path_tags = array();
preg_match_all('#'.LD.'(filedir_([0-9]+))'.RD.'#', $TMPL->tagdata, $path_tags);
foreach($path_tags AS $tag)
{
if(isset($FNS->file_paths[$tag[2]]))
{
$TMPL->tagdata = $TMPL->swap_var_single($tag[1], $FNS->file_paths[$tag[2]], $TMPL->tagdata);
}
}
Thanks will give that a shot.
My original code does work and yes, it’s nested within a weblog tag. That just got me thinking … could I output “https://ellislab.com/asset/images/ent-partner-logo/filename.pdf” from my plugin and would it automatically be replaced on rendering? Must give that a shot!
I’m nervous about accessing the $FNS variables directly in the event that EE might change them for whatever reason. That’s why I prefer to use methods/API calls instead as they are usually considered more “rigid” by developers and would change far less frequently.
Thanks, Af.
I was running into the need (or wish, anyway…) to be able to use the {filedir} variables on a template So, there is a nice clean plugin that I can now make use of, Afro Boy? Suggested look:
{exp:filedir file=
or
{exp:filedir dir=
Resulting in something like:
/images/uploads/samplepics/sample.jpg
Unless there is an easier way I’m not thinking of, of course.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.