Jump to content

Automatic page name using ProcessPageAdd::execute and PageTable


POWERFULHORSE
 Share

Recommended Posts

I'm trying to automatically generate the title and name fields when a new item is added to the PageTable. I've used some code renobird shared with me and Pete's code from this post but it won't quite work for what I need to do.

So for example I have a page /foo/bar/title-of-the-download, this contains a PageTable field that lists downloads. The pages created by this field are stored in /downloads/.

I want to use the format: 150818-name-01 (date-name-increment) where name is taken from the page which contains the PageTable. So using the example above, I could add several new items to the PageTable with the names automatically created as:

150818-title-of-the-download-1

150818-title-of-the-download-2

150818-title-of-the-download-3

I'm able to get the name and title of the page I'm storing the PageTable pages under (in this case downloads), but not the page which contains the PageTable field. Can anyone advise on how I might do this? Code so far below:

 

<?php
 
class PageRename extends WireData implements Module {
 
        public static function getModuleInfo() {
                return array(
                        'title' => 'PageRename',
                        'summary' => 'Page Rename Module',
                        'href' => '',
                        'version' => 001,
                        'autoload' => true,
                        'singular' => true,
                );
        }
 
        public function ready() {
                $this->pages->addHookBefore('ProcessPageAdd::execute', $this, 'hookFormatName');
        }

        public function hookFormatName(HookEvent $event) {
                //if adding child page to /downloads/ (1456)
                if ($this->input->get->parent_id == 1456) {
                        //how to get origin page?
                        $originPage = ;
                        //gets number of child pages under /downloads/
                        $increment = $this->pages->get($this->input->get->parent_id)->count()+1;
                        //actual increment needed
                        $increment =  $originPage->children()->count()+1;
                        //create new page
                        $page = new Page();
                        $page->parent = $this->input->get->parent_id;
                        $page->template = 'download';
                        $page->name = date('ymd').'-'.$originPage->name.'-'.$increment;
                        $page->title = date('ymd').'-'.$originPage->title.'-'.$increment;
                        $page->addStatus(Page::statusUnpublished);
                        $page->save();
                        $this->session->redirect("../edit/?id=$page");
                }
        }
}
  • Like 1
Link to comment
Share on other sites

You won't get the page which holds the page-table from hooking ProcessPageAdd, except if it's the parent page of the created one. ProcessPageAdd does simply add a new page and doesn't know anything about the page-table, that invoked the process.

Link to comment
Share on other sites

You won't get the page which holds the page-table from hooking ProcessPageAdd, except if it's the parent page of the created one. ProcessPageAdd does simply add a new page and doesn't know anything about the page-table, that invoked the process.

Thanks LostKobrakai, I thought that might be the case. So looks like I need to look at using hooks with some different events/methods in order to make this work?

Link to comment
Share on other sites

So I can't figure this one out at all. Is there some other method that I can hook that occurs when the add page button is clicked? It seems everything in the Pages class happens after you click Save. Am I right in saying your first suggestion would require me to edit files in the /wire/ directory?

Link to comment
Share on other sites

I'm not sure if there is any hookable function, that is called at the right time. If there isn't than you can either asked for one over at Github or implement something on your own – rather copy the module in the site/modules/ directory, instead of changing anything in wire/.

Link to comment
Share on other sites

  • 1 year later...
On 8/18/2015 at 10:25 AM, LostKobrakai said:

Yeah, you would either need to hook to where ProcessPageAdd is called and change the arguments passed to it there. Or you need to adjust the name after the page is created, but before/while it's added to the pagetable.

Is it possible to rename the page after it's been added to the Page Table?

In the admin interface there is an "Add New" button for Page Table fields. I am trying to set the name of the page being created based on content that is entered into the page in the "Add New" overlay dialog. Is that possible without editing ProcessWire source code?

It seems like the name has already been entered into the PageTable before the new page is created and that might be why hooking 'setupPageName' is not effective.

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