bmacnaughton

How to capture page containing a pagetable?

9 posts in this topic

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?

Share this post


Link to post
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
2 people like this

Share this post


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

Share this post


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

4 people like this

Share this post


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

Share this post


Link to post
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
    // ...
});

 

2 people like this

Share this post


Link to post
Share on other sites

Thank you @Robin S. I am going to give this a try - I solved this particular problem by forcing the new entries under the page they are contained by but that is a suboptimal solution. 

Share this post


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

1 person likes this

Share this post


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

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 beto
      I can't add a new value to a custom PageArray field  for my user.
       
      However, when I create a new user, I CAN add the new PageArray value:
       
      I need help. Thank you very much.
       
    • By bmacnaughton
      I would like to be able to fetch the labels for fields in a language different than the current logged in user.
      For field values that's easy 
      // p is page, l is language, and f is field $p->getLanguageValue($l, $f); I'm looking for something like
      $fields->getLanguageValue('en', 'length'); The only solution I know of is to save the current user language, iterate through the languages by setting the user language and fetching the value, and then restore the user language.
       
    • By bmacnaughton
      Is there a way to tell if a field is a multi-language field?
      I am currently checking to see if the last 8 characters are 'Language' but that seems like a fragile solution.
       
       
    • By louisstephens
      I was working on a simple "to-do" style template that has a form on a page. Once the form is submitted, I use the API to  create a new subpage under a pre-existing page, but I notice that it does resubmit the form data (as to be expected) if a user were to refresh the page. Is there a way to prevent this behavior so duplicate content will not be posted by using something like exit(), or is this not proper due to usability?  I guess I am curious as to how other developers handle the same hurdle.
    • By Andreas Augustin
      Hello!
      How can I add custom Content to the Admin Theme Footer or Header?
      Is this possible with a hook? I want also to can upgrade easy and don't touch core files...