We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Accessing filedir_1 path through a plugin ...

Development and Programming

Afro Boy's avatar
Afro Boy
20 posts
17 years ago
Afro Boy's avatar Afro Boy

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.

       
Robin Sowell's avatar
Robin Sowell
13,160 posts
17 years ago
Robin Sowell's avatar Robin Sowell

This is one for the plugins tech forum- and in truth I don’t know the best way to handle it. I can give the devs a heads up on the thread- I’d be curious to see what they recommend. Does the above work? And are you nesting the plugin inside another tag?

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

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);
    }
}
       
Afro Boy's avatar
Afro Boy
20 posts
17 years ago
Afro Boy's avatar Afro Boy

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.

       
Ryan M.'s avatar
Ryan M.
1,511 posts
17 years ago
Ryan M.'s avatar Ryan M.

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.