Jump to content

Recommended Posts

Posted

Hello guys,

I am setting up a page for news publishers to be able to upload PDFs. Some forum browsing helped a lot with gile uploads (thanks, Ryan!).

However, I ran into a different kind of problem: I can add files to pages just fine but I do not know how to replace/delete them after!

Let's say I have uploaded a file to a page. I can see it in the admin backend. Then I try to delete it using an API call ("file" is the name of a file field that I added to the basic-page template):

$wire->pages->get("$refererPageId")->file->pagefiles->delete();

Doesn't work. Says, Error Call to a member function delete() on a non-object

$wire->pages->get("$refererPageId")->file->delete();

does not work either.

I've run out of ideas, and looking through the code did not help (I know php just enough to be able to use pre-defined functions).

Could you please enlighten me on how to delete page files using the API

Thanks a lot in advance.

Posted

Are you sure you named the file-field "file"?

Because I guess the syntax is like:

$wire->pages->get($refererPageId)->nameOfYourFileField->delete();
Posted

Nico, yes. I even renamed the field to something inconspicuous but it did not help.

I try

$wire->pages->get("$refererPageId")->nn_basic_page_file->delete();

($refererPageId equals 1001 in this case)

However, PW says:

Error Uncaught exception 'WireException' with message 'Invalid type to Pagefiles::remove(item)' in /u/www/htdocs/wire/core/Pagefiles.php:189
Stack trace:
#0 /u/www/htdocs/wire/core/Pagefiles.php(175): Pagefiles->remove(NULL)
#1 [internal function]: Pagefiles->___delete()
#2 /u/www/htdocs/wire/core/Wire.php(271): call_user_func_array(Array, Array)
#3 /u/www/htdocs/wire/core/Wire.php(229): Wire->runHooks('delete', Array)
#4 /u/www/htdocs/f.php(68): Wire->__call('delete', Array)
#5 /u/www/htdocs/f.php(68): Pagefiles->delete()
#6 {main}
thrown (line 189 of /u/www/htdocs/wire/core/Pagefiles.php)
Posted

Have a look into the cheatsheet and search for delete,

you get something like this: $files->delete($file)

damn Antti was faster

  • Like 2
Posted
Have a look into the cheatsheet and search for delete,

you get something like this: $files->delete($file)

damn Antti was faster

Thanks, Luis. What I am not getting is what kind of variable $files is. 

is it $page->files?

just $files?

@Apeisa,

Thanks for the hint. I tried something like

$wire->pages->get(1001)->nn_basic_page_file->pagefiles->delete(); 

The API cheatsheet says that pagefiles returns a $files array. But I keep getting the same error:

Error Call to a member function delete() on a non-object

Maybe somebody has a working code snippet that deletes a page file through the API?

Posted
$wire->pages->get(1001)->nn_basic_page_file->pagefiles->delete(); 

Where do you get that pagefiles? Also, if you use delete(), you need to specify which file you are removing.

This might be what you are looking for:

$wire->pages->get(1001)->nn_basic_page_file->deleteAll(); 
  • Like 1
Posted
$wire->pages->get(1001)->nn_basic_page_file->pagefiles->delete(); 

Leave out pagefiles, the name of your field is "nn_basic_page_file"

Try this:

//Delete the first file
$firstFile = $wire->pages->get(1001)->nn_basic_page_file->first();
$wire->pages->get(1001)->nn_basic_page_file->delete($firstFile);
  • Like 2
Posted

apeisa, Wanze,

Thanks a lot for your help. deleteAll() worked, of course.

As did

foreach( $wire->pages->get(1001)->nn_basic_page_file as $value )
{

$wire->pages->get(1001)->nn_basic_page_file->delete($value);
}
 
However, I am still trying to understand how to delete files by ID, not by name. I know that $wire->pages->get(1001)->nn_basic_page_file is an array. Is there a way to access it by numeric keys? Like $wire->pages->get(1001)->nn_basic_page_file[0], for example?
Posted
However, I am still trying to understand how to delete files by ID, not by name. I know that $wire->pages->get(1001)->nn_basic_page_file is an array. Is there a way to access it by numeric keys? Like $wire->pages->get(1001)->nn_basic_page_file[0], for example?

nn_basic_page_file contains Pagefiles object, which is inherited from WireArray, which is actually an object that PW uses pretty much everywhere. It is neat concept and makes working much faster than with regular PHP arrays. See more from here: http://processwire.com/api/arrays/

So from that link you find all the ways to find files from your field. Some examples:

//first, this makes it all nicer:
$p = wire('pages')->get(1001);

$first_file = $p->nn_basic_page_file->first(); // like wanze showed above
$second_file = $p->nn_basic_page_file->eq(1);
$random_file = $p->nn_basic_page_file->getRandom();
$specific = $p->nn_basic_page_file->get("filename=myphoto.jpg");
  • Like 4
Posted

Thanks a lot, mate! eq() was just the exact method I was after,

Hope your expanation will help others like me who mix up arrays and objects :)

  • Like 1
Posted
What's eq() by the way?

If I'm right, it's the same as ->get($key)

From the cheatsheet: Returns the single item at the given zero-based index, or NULL if it doesn't exist.

eq = equals I guess

  • Like 1

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
×
×
  • Create New...