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 bmacnaughton
      When I delete a page name, e.g, /cart/, using the admin interface it goes into trash and gets the name /trash/2573.1.11_cart/ .
      I see that, with the Pages::trashed hook that the previous page name (previousPage) can be accessed.
      Where can I more information about what happens when a page is put into trash and what the name means?
    • By bmacnaughton
      I have a hook that creates a page for a subset of the pages on our site. It uses the saved page's name as part of the created page's name.
      The problem I am having is that my hook, attached to Pages::saved(), is being called even when the page save failed because of missing fields. Is there a way I can tell that the page save failed due to missing fields?
       
      Never mind - it does succeed; it just issues warnings about required fields.
       
    • By Harmen
      Hi folks,
       
      On the website I am working on I do have a form which needs to be filled in (duh) and when the user clicks on download, the data is send with ajax and processed in the same file by using the ProcessWire API with $config->ajax;
      The data which is provided by the user will be checked with data in my database. If the data is correct I want to push a file download. But this is not working at the moment. It seems like the content of the (.exe) file is pasted in the console instead of pushing the file. My code looks like this:
      if ($config->ajax){ $serial = $input->post('serial'); $DB = DB(); $query = $DB->prepare("/*My query here*/"); $query->bindParam("comparision", $serial, PDO::PARAM_STR); $query->execute(); $reply = ''; $SN_rows = $query->rowCount(); if (!$SN_rows > 0) { $reply = 'error'; } else { $reply = 'success'; $installer = $page->attachments->eq(1); $filename = "Filename"; $filepath = $installer->filename; $options = array( 'forceDownload' => true, 'exit' => true, 'downloadFilename' => $filename ); wireSendFile($filepath, $options); } echo $reply; exit(); } What am I doing wrong here or is it a bug? Is there another way to do this with the processwire API?
       
      Thanks in advance,
       
      ~Harmen
    • By nbcommunication
      Hi,
      I'm sure this is maybe in the works already, given that findMany() is a recent addition to the API, but having this (and the other new find options) available to $users would be a great addition.
      Cheers,
      Chris
      NB Communication
       
    • By Kiwi Chris
      I'd really like Processwire to have a robust page based permissions system rather than just template based out of the box.
      If the page tree is compared to a file system, it would be nice to be able to set owner (user), group (role) and public permissions on a page and have this propagate to sub-pages unless explicitly over-ridden.
      With a large site, it's quite possible to have multiple sections that use the same templates, but where editing needs to be assigned to different people, for different parts of a site.

      I've seen some proofs of concept that have never been updated or maintained, so I'm sure it can be done, but this is a bit of a show-stopper for me with an otherwise excellent CMS.
      I might be able to have a go at something myself, but since this is security related, and I haven't had a go at module development before, it would be really handy to have something robust that just works.