Jump to content

How to capture page containing a pagetable?


bmacnaughton
 Share

Recommended Posts

When a PageTable field in a specific template is being edited I need to know the Page that contains the PageTable so I can fill in hidden fields in the PageTable.

I can capture the page being edited via:

// $this->addHookBefore('ProcessPageEdit::execute', ...

public function pageEditExecuteBefore(HookEvent $e) {
    $p = $e->object->getPage();
    if ($p->template !== 'rtw-product') return;
    // $p is the page being edited
}

I can intercept PageTable entry being saved:

// $this->addHookBefore('Pages::save'

public function savePageBefore (HookEvent $e) {
    $p = $this->wire('page')->id;
    $page = $e->arguments('page');
    $obj = $e->object;
    $name = $page->name;                       // page name of PageTable page
    $template = $page->template->name;         // template of PageTable page
    $parent = $page->parent->name;             // parent directory for PageTable items

What I am trying to find is the page in which the PageTable field is located.

I've also tried having pageEditExecuteBefore() saving $p in $this->context and then accessing that in savePageBefore() but it's a different instance of the class because $this->context is null when it gets to savePageBefore(). I could save the page ID in session, but that seems error prone.

Does anyone know how to achieve this?

Link to comment
Share on other sites

Try this

$fieldList = $fields->find('type%=PageTable');
foreach($fieldList as $f) {
    if($page->template->hasField($f)) {
        // page has the field
    }
}

Or simply:

if($page->fields->find('type%=PageTable')->count)) {
	// page has a PageTable field
}

// check for a specific field
if($myField = $page->fields->get('type%=PageTable')) {
	// $myField of PageTable type exists on this page.
}

Here I used find() method with a selector for matching type. You can be more specific, or use any other WireArray methods of course.

https://processwire.com/api/ref/wire-array/

Edited by abdus
Added a simpler way
  • Like 2
Link to comment
Share on other sites

Thanks Abdus. I have a little different problem - I am looking for the specific page instance that has the field being edited. There are multiple pages based on the template that contains that field. I need to know which specific page instance is the "logical parent", not the set of pages that contain that field, i.e., are instances of the template that includes that PageTable field.

To try to make it more concrete:

PageA, PageB, and PageC all have the PageTable field Variations.

When in the Admin interface PageA is being edited and the user wants to add a variation then they click "Add New" under Variations.

At that point an iFrame overlay pops up to enter data for the new PageTable page in Variations.

When that PageTable page is saved I need to know that it is associated with PageA.

If the PageTable pages were in their default location (children of PageA) then I could just look at parent. But the PageTable pages are all kept in a single directory for grouping purposes.

Make sense?

Link to comment
Share on other sites

usually what i do in these cases is add a page field to the template of the pagetable page, and set that field to the page that it is being edited from. I used a save hook on the main page to look at the items in the page table and for any where that page field is not set, it assigns that field to the page being edited (so setting the page field on the pagetable item to PageA). that field is also hidden using CSS.

  • Like 4
Link to comment
Share on other sites

Another way perhaps:

When you look in the database at field_pagetablefieldname you will see 3 columns -

pages_id = the page on which the PageTable appears (this is what you need)

data = the id of the PageTable page

sort = sort order for the items to appear on the pages_id page

You could hook after the new PageTable entry is saved (ie Pages::saved), and do a $database call to select the pages_id that has the newly created page id in the data field.

 

Screenshot 2017-05-04 09.37.05.png

Link to comment
Share on other sites

@bmacnaughton, give the following hook a try (must be in /site/init.php or in init method of module). Seems to work well for both PageTable additions and edits of existing items.

$this->addHookBefore('InputfieldPageTableAjax::checkAjax', function($event) {
    $page_id = (int) $this->input->get('id');
    $item_id = (int) $this->input->get('InputfieldPageTableAdd');
    if($page_id) $page = $this->pages->get($page_id); // $page is the container page
    if($item_id) $item = $this->pages->get($item_id); // $item is the PageTable item
    // ...
});

 

Edit Nov 2017: more information in this post...

 

Edited by Robin S
Link to related post
  • Like 2
Link to comment
Share on other sites

You're all wrong and most only work with entries already added and saved to the pagetable. It doesn't work for when adding a new item, cause the new entry page isn't saved yet to the pagetable field... Only easy way is to store them as children, so you can check for the parent. But storing the entries in a different place there's no way to tell in the newly created entry what source page it is going to land in. 

I created a github issue 3! years ago https://github.com/ryancramerdesign/ProcessWire/issues/699 and never got a single response from Ryan...

The solution I came up with was to store the id in the session and transfer that over to the page edit of the entry, Since I had not autoname feature enabled there, there was a step in between where the entry page isn't even created yet. Kinda hacky but worked.

The other issue I got with PageTable also never got answered https://github.com/ryancramerdesign/ProcessWire/issues/700

  • Like 1
Link to comment
Share on other sites

7 hours ago, Soma said:

The solution I came up with was to store the id in the session and transfer that over to the page edit of the entry,

I tried the same thing - it was the last way I could think of. I'm not sure why it didn't work but will try again.

Link to comment
Share on other sites

  • 1 month later...

yeah, to achieve what i was referring to above, i had a hook on the save of the main page, which iterated through all of the items in the pagetable where the field was blank, and set that value...

Has anyone come up with a reliable way to get the ID of the page that contains the page table field, either through hook, database query, or session vars? would be very helpful to know if this has been solved - maybe @Soma could you post an example of how to set the id of the page in the session; my issue with this is i have a page in a page table, which then has another page table in it, so the system is having trouble with any get var because it ends up being the root/bottom page, not the 2nd level up modal which is the page id i need...

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...