Jump to content

Need help with subfield selectors for Files


Valery
 Share

Recommended Posts

Hello, everybody!

I've been experimenting with manipulating files through the API recently and I cannot get my selectors to work with Files.

I read about subfield selectors but I cannot get the following code to work:

$myFile = $page->get("basic_page_file.name=6121970044ELEPHPANT.jpg"); // field basic_page_file is of type "Files".

if (!$myFile)
    echo "Nothing was found"; // does not get displayed

echo "File name is " . $myFile->name . "<br />"; // nothing is displayed for '$myFile->name'
 

Actually, neither error nor the name of the file get displayed.

So, I am curious as to what the correct way to use subfield selectors with Files is?

Link to comment
Share on other sites

When searching pages with images field, there's no subfield "name" for images, but you can use images.data. And you won't get the image but a page(s)

$p = $wire->pages->get("images.data%=mode"); // get return the first match
echo $p->images->get("filename%=mode")->url;

Or 

$p = $wire->pages->find("images.data=modelifestyle.jpg")->first(); // returns a pagearray and we access first
echo $p->images->get("filename%=mode")->url;

Or 

echo $p->images->get("name%=mode")->url;

Or 

echo $p->images->find("filename%=mode")->first()->url;

Or

echo $p->images->get("modelifestyle.jpg")->url;

Or

echo $p->images->get("modelifestyle.jpg")->name; // image name

or

echo $p->images->get("modelifestyle.jpg")->filename; // full path
  • Like 7
Link to comment
Share on other sites

Very extensive--thanks! While this is quite helpful, I was wondering if there is a way to rename files and images from the API. But as you have pointed out in your reply there is hardly any option besides updating the database manually.

Link to comment
Share on other sites

$p = $wire->pages->get("images.data%=mode");

All fields are assumed to have at least a "data" field, so you can also query it just by specifying the field name without "data", i.e. 

$p = $pages->get("images%=mode"); 
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...