Jump to content
leoric

how to delete a page in page field [Multiple pages (PageArray)]

Recommended Posts

hi,everyone!

i could add some pages in page field like this:

$user->user_app->add($page1);

$user->user_app->add($page2);

$user->user_app->add($page3);

...etc

$user->save();

 now the issue is  i dont know how to delete one page. 

thanks!

Share this post


Link to post
Share on other sites

ok,i did it like this:

$user->user_app->removeAll($page);

its working, is it right?

Share this post


Link to post
Share on other sites

I think this should work but you can also do

$user->user_app->remove($page);
  • Like 1

Share this post


Link to post
Share on other sites

I think this should work but you can also do

$user->user_app->remove($page);

hi ,soma 

before this i had tried to use "remove" ,but it does not work. so i change to "removeAll"

and i found :

if there have this pages in page field [Multiple pages (PageArray)]:

page1 , page2, page 3..etc

removeall() : will remove all pages      =>    its worked      :rolleyes:

removeall(page1): page1 will be removed. => its worked   :rolleyes:

remove(page1):  nothing changed..   => its not working   :(

Share this post


Link to post
Share on other sites

I just looked into this again and there's no argument for removeAll() just for remove($key). 

See here in source https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/WireArray.php#L790

removeAll() removes ALL pages from a Wire/PageArray (pages, images, POST vars etc). So I'm not sure about why it works for you because it shouldn't and also doesn't work for me.

Assuming user_app is a page field allowing multiple pages to be selected. It's a WireArray/PageArray and you add pages to it like this from API:

$user->of(false);
$page1 = $pages->get("/somepage/");
$page2 = $pages->get("/someotherpage/");
$user->user_app->add($page1);
$user->user_app->add($page2);
$user->save();
 

Now we got 2 pages added to the page field.

If you now do a $page->user_app->removeAll($page1) they all get removed so does $user->user_app->removeAll();

But if you do:

$page2 = $pages->get("/someotherpage/");
$user->user_app->remove($page2);
$user->of(false);
$user->save();
 

Now only the $page2 which is in the array gets removed. It works all as expected and make sense.  :rolleyes:

  • Like 6

Share this post


Link to post
Share on other sites

I just looked into this again and there's no argument for removeAll() just for remove($key). 

See here in source https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/WireArray.php#L790

removeAll() removes ALL pages from a Wire/PageArray (pages, images, POST vars etc). So I'm not sure about why it works for you because it shouldn't and also doesn't work for me.

Assuming user_app is a page field allowing multiple pages to be selected. It's a WireArray/PageArray and you add pages to it like this from API:

$user->of(false);
$page1 = $pages->get("/somepage/");
$page2 = $pages->get("/someotherpage/");
$user->user_app->add($page1);
$user->user_app->add($page2);
$user->save();
 

Now we got 2 pages added to the page field.

If you now do a $page->user_app->removeAll($page1) they all get removed so does $user->user_app->removeAll();

But if you do:

$page2 = $pages->get("/someotherpage/");
$user->user_app->remove($page2);
$user->of(false);
$user->save();
 

Now only the $page2 which is in the array gets removed. It works all as expected and make sense.  :rolleyes:

@soma ,your reply is very helpful...

my fault: $app is a page id variable, not a $page object.

$removepage = $pages->get($app);          ->  add this line
$user->user_app->remove($removepage);   ->change removeall  to remove   
 
its working ^-^
 

thank you very much!

  • Like 1

Share this post


Link to post
Share on other sites

Hey in PW 2.4 I was having a PageArray field assigned to a template. And the operations like has, remove etc

$page->page_array_field->has($id);

$page->page_array_field->remove($id);

If the value is integer it don't remove the value or find a value.  But only passing a real page object removes the value from the db. Or is it still behaving for everyone else ?

Share this post


Link to post
Share on other sites

I'm having the same problem. Yet I can not find a solution. How do I change my integer into a real object?

This is my code. According to the docs this should work. Right?

                $u->projectmanagers->add(1044); // THIS WORKS!
                $u->projectmanagers->remove(1044); // THIS DOESNT WORK!
                $u->save();

Only thing I can think of doing is first store the array, then remove all, then remove the one item from the array and then adding the array again. But I'd rather not ;)

Edited by mcwhitey
Wanted to add something

Share this post


Link to post
Share on other sites

You can add pages using page objects or page ids, but can only remove them using page objects. A bit inconsistent, to be honest.
So you need to do

$u->projectmanagers->remove($pages->get(1044));

From the core:

// PageArray.php

public function add($page) {
    // ...
    } else if(ctype_digit("$page")) {
        $page = $this->wire('pages')->get("id=$page");
        if($page->id) {
            parent::add($page); 
            $this->numTotal++;
        }
    }
    return $this;
}

public function remove($key) {

    // if a Page object has been passed, determine its key
    if($this->isValidItem($key)) {
        $key = $this->getItemKey($key);
    } 
    if($this->has($key)) {
        parent::remove($key);
        $this->numTotal--;
    }

    return $this; 
}

Edit:

I created a feature request

  • Like 2

Share this post


Link to post
Share on other sites

Thanks Abdus,

Can't say I fully understand but it does work now! Awesome! Good idea to submit a feature request :)

Cheers!

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By onjegolders
      Hi guys, have been struggling all day with a problem creating a front-end form.
      The idea is that "students" are linked with "feedback" and on both templates there is a page field which links the two together.
      The superuser has an update feedback form on the frontend where they can make changes, set to hidden/visible as well as assigning the feedback to a student.
      For some reason I can get the feedback page itself to be updated, I can remove any old attachments from student pages to this feedback page but trying to update the new student to be attached is causing errors. 
      I've had an error telling me to set OutputFormatting to false (it already is) and now I seem to be getting server errors that it's running out of time.
      I've included the current commented code below, can anyone see anything I'm missing?
      Thanks
      <?php // If form is submitted if ($input->post->submit_update_testimonial) { // Assign empty variables $out = ""; $error = 0; // Check if required fields empty if (empty($input->post->title) || empty($input->post->testimonial)) { $error = 1; $out = "<h3>Please include a title and testimonial</h3>"; } if ($error == 0) { // Set sanitized variables $title = $sanitizer->text($input->post->title); $testimonial = $sanitizer->textarea($input->post->testimonial); $date = $sanitizer->text($input->post->date); $student = $sanitizer->text($input->post->student); // Get current student who is attached to feedback (if any) and remove the attachment $current_s = $pages->get("template=student, student_feedback=$page"); if ($current_s->id) { $current_s->of(false); $current_s->student_feedback = ""; $current_s->save(); } // See if newly-selected student exists and if so attach it to this feedback (CAUSING ERRORS) $new_s = $pages->get("template=student, id=$student"); if ($new_s->id) { $new_s->of(false); $new_s->set("student_feedback", $student); $new_s->save(); } // Update the actual feedbcak contents from the form $page->of(false); $page->title = $title; $page->body = $testimonial; $page->testimonial_date = $date; // Update whether the feedback should be visible or hidden if ($input->post->publish == "") { $page->addStatus(Page::statusHidden); } else { $page->removeStatus(Page::statusHidden); } // If set to a student then assign it on the feedback page if ($student == "unassigned") { $page->set("feedback_which_student", ""); } else { $page->feedback_which_student = $student; } $page->save(); } // end if no errors } // end if form submitted ?> <?php if(!$user->isGuest() && $user->name === $page->name OR $user->isSuperuser() OR $user->hasRole("editor")) { include("./header.inc"); ?> <div id="feedback_item_admin" class="content"> <div class="row"> <div class="large-12 columns"> <div class="content_area"> <div class="row"> <div class="large-9 columns"> <h3>Edit Testimonial</h3> </div> <!-- /.large-9 columns --> <div class="large-3 columns"> <a href="<?php echo $page->parent->url; ?>" class="tar flr">← Return to Testimonials</a> </div> <!-- /.large-3 columns --> </div> <!-- /.row --> <hr> <form action="./" method="post"> <div class="row"> <div class="large-8 columns"> <input type="text" name="title" value="<?php echo $page->title; ?>"> </div> <!-- /.large-8 columns --> <div class="large-4 columns"> <input type="date" name="date" value="<?php echo date('Y-m-d', strtotime($page->testimonial_date)); ?>"> </div> <!-- /.large-4 columns --> </div> <!-- /.row --> <textarea name="testimonial" cols="30" rows="10"><?php echo strip_tags($page->body); ?></textarea> <div class="row"> <div class="large-6 columns"> <label for="publish">Publish?</label> <input type="checkbox" name="publish"<?php if (!$page->is(Page::statusHidden)) { echo " checked"; } ?>> </div> <!-- /.large-6 columns --> <div class="large-6 columns"> <label for="student">Relates to student?</label> <select name="student"> <?php if ($page->feedback_which_student->id != "") { ?> <option value="<?php echo $page->feedback_which_student->id; ?>"><?php echo $page->feedback_which_student->title; ?></option> <?php } ?> <?php foreach ($pages->find("template=student")->remove($page->feedback_which_student) as $student) { ?> <option value="<?php echo $student->id; ?>"><?php echo $student->title; ?></option> <?php } ?> <option value="unassigned"<?php if ($page->feedback_which_student->id == "") { echo " selected"; } ?>>Unassigned</option> </select> </div> <!-- /.large-6 columns --> </div> <!-- /.row --> <input class="button secondary small drop_down" type="submit" name="submit_update_testimonial" value="Update"> </form> </div> </div> </div> <?php include("./footer.inc"); ?> <?php } else { throw new Wire404Exception(); }
    • By NooseLadder
      Is it possible to change the template on multiple pages quickly? I have many pages using the same template and I want to change them all to another template, all the same.
×
×
  • Create New...