Jump to content

Use API to remove item by id from repeater on a specific page


PascalKonings
 Share

Recommended Posts

After searching through this forum and not being able to find a working solution, thought I'd try it here. I must be overlooking something really simple.

I have a template with a repeater field, containing a number of items.

I want to use the API to remove one of these items, based on its ID, save the repeater (if necessary) and then save the page on which it resides.

// The page with template "agenda"
$agenda = wire('pages')->find("template=agenda")[0];				

// The event field is the repeater field here
// Getting the repeater item by ID
$event = $agenda->event->get('id='.$id);

// Disable output formatting
$agenda->of(false);

// Remove the event item from the event repeater
$agenda->event->remove($event);

// Not sure if this is needed
// $agenda->event->save();

// Save the page
$agenda->save();  

 

Sofar I'm getting this error:

Trying to get property 'id' of non-object

Which seems to be because of the remove action:

$agenda->event->remove($event);

If I remove this line, all seems to work as intended, but obviously the repeater items is not being removed.

What am I missing here?

Link to comment
Share on other sites

My guess would be that it's the line getting a value for $event where things start to go wrong.

I'm wondering in particular where you're getting the value of $id from, and whether it's valid?

You might want to try var_dump() to check whether $event actually is an object (and if so, if it looks as if it might be a repeater item):

var_dump($event);

 

 

  • Like 1
Link to comment
Share on other sites

Repeater fields give you a PageArray. Have you tried getPageByID($id) to retrieve the item? Check the return value before removing. It might be NullPage, or when using get() it might be null.

As a matter of fact, since you already know the ID, it should be perfectly safe to just delete the page:

$pages->get($id)->delete();

 

  • Like 1
Link to comment
Share on other sites

On 5/13/2022 at 4:28 PM, BillH said:

My guess would be that it's the line getting a value for $event where things start to go wrong.

I'm wondering in particular where you're getting the value of $id from, and whether it's valid?

You might want to try var_dump() to check whether $event actually is an object (and if so, if it looks as if it might be a repeater item):

var_dump($event);

 

 

Hi Bill, yes it is an object and contains an id and all other information in the repeater field. Deleting it using @Jan Romero suggestion still throws me an error in the module "App API" that I'm using to delete this item:

devmessage: {message: "Trying to get property 'id' of non-object", location: '/Applications/MAMP/htdocs/hkg/wire/core/PagesEditor.php', line: 296}
error: "Internal Server Error"

However deleting it from PHP without the module using:

wire('pages')->get(1372)->delete();

seems to work fine. And it is the exact same code as in my App API endpoint. Must be a bug in the module then...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...