Jump to content

Module which copies the values of a parent page to the child page by using a pagetable field


Juergen
 Share

Recommended Posts

Hello @ all

I want to share my code of a module which copies values from a parent page to a child page by using the add button of a pagetable field.

If you find it useful you can copy the code or you can improve the code and post your ideas of improvement here.

The intention for me was that I have pages with events and I dont want to write all the data for an event manually. Especially if only the date of the event is different.

I use a pagetable field for the events and I want to click the add button of this field and a new childpage will be created with all the data of the event (title, description, summary,...) so I have only to fill in the start and end date for the event.

So far so good, but it was a little bit tricky to get this to work with the add button of the pagetable field. So I decided to write a module which does the work for me.

Here is the piece of code:

<?php
class CopyPageTableAdd extends WireData implements Module
{
    public static function getModuleInfo()
    {
        return array(
            'title' => 'Pagetable copy values',
            'summary' => 'Copy Pagetable content by pressing the add button',
            'href' => '',
            'version' => 001,
            'autoload' => true,
            'singular' => true
        );
    }
    public function ready()
    {
        $this->pages->addHookBefore('ProcessPageAdd::execute', $this, 'copyaddpage');
    }
    public function copyaddpage()
    {
        //configuration
        $parenttemplatename = "events"; //the template name of the parent template
        $childtemplatename  = "single-event"; //the template name of the newly created child template        
        $page               = new Page();
        $page->parent       = $this->input->get->parent_id;
        if ($page->parent->template == $parenttemplatename) {//check if it is the right parent template
            $page->template             = $childtemplatename; //set the template for the created child page
            //copy all fields field values from the parent template (start)
            //enter all fields which you want to copy into the child page
            $page->title                = $page->parent->title;
            $page->eventstatus          = $page->parent->eventstatus;
            $page->eventtype            = $page->parent->eventtype;
            $page->summary              = $page->parent->summary;
            $page->headline             = $page->parent->headline;
            $page->importanteventstext  = $page->parent->importanteventstext;
            $page->importanteventstext  = $page->parent->importanteventstext;
            $page->notifiable           = $page->parent->notifiable;
            $page->reservationtype      = $page->parent->reservationtype;
            $page->participantlimit     = $page->parent->participantlimit;
            $page->participantmaxnumber = $page->parent->participantmaxnumber;
            $page->eventmaxstatus       = $page->parent->eventmaxstatus;
            $page->eventcosttype        = $page->parent->eventcosttype;
            $page->eventprice           = $page->parent->eventprice;
            $page->eventpriceadd        = $page->parent->eventpriceadd;
            $page->eventlocationname    = $page->parent->eventlocationname;
            $page->street               = $page->parent->street;
            $page->postalcode           = $page->parent->postalcode;
            $page->eventlocationname    = $page->parent->eventlocationname;
            $page->place                = $page->parent->place;
            $page->region               = $page->parent->region;
            $page->country              = $page->parent->country;
            $page->googlemap            = $page->parent->googlemap;
            //copy all fields field values from the parent template (end)
            $page->addStatus(Page::statusUnpublished);
            //this foreach loop is only if you have a multilanguage site to get the path names in each language
            //if your site is only single language you can use $page->name=$page->parent->name (but not tested)
            foreach ($this->languages as $lang) {
                $lname    = $lang->id;
                $pageName = $page->title->getLanguageValue($lang);
                $page->set("name$lang", $pageName);
                if ($lang->isDefault())
                    continue;
                $page->set("status$lang", 1);//activate the multilanguage checkbox
            }
            $page->save();
            $this->session->redirect("../edit/?id=$page");
        }
    }
}

The configuration part:

//configuration
        $parenttemplatename = "events"; //the template name of the parent template
        $childtemplatename  = "single-event"; //the template name of the newly created child template

This is the part where you have to define the templates. The name of the parent  template is responsible that the module only run on that template (in my case the template with the name "events). The name of the child template ist the template which should be created after pressing the add button (in my case the template with the name "single-event").

You have to fill in your template names.

The module runs only if the parent template has the specific name and creates only child pages with the child pages template name.

Copy all field values par:

            //copy all fields field values from the parent template (start)
            //enter all fields which you want to copy into the child page
            $page->title                = $page->parent->title;
            $page->eventstatus          = $page->parent->eventstatus;
            $page->eventtype            = $page->parent->eventtype;
            $page->summary              = $page->parent->summary;
            $page->headline             = $page->parent->headline;
            $page->importanteventstext  = $page->parent->importanteventstext;
            $page->importanteventstext  = $page->parent->importanteventstext;
            $page->notifiable           = $page->parent->notifiable;
            $page->reservationtype      = $page->parent->reservationtype;
            $page->participantlimit     = $page->parent->participantlimit;
            $page->participantmaxnumber = $page->parent->participantmaxnumber;
            $page->eventmaxstatus       = $page->parent->eventmaxstatus;
            $page->eventcosttype        = $page->parent->eventcosttype;
            $page->eventprice           = $page->parent->eventprice;
            $page->eventpriceadd        = $page->parent->eventpriceadd;
            $page->eventlocationname    = $page->parent->eventlocationname;
            $page->street               = $page->parent->street;
            $page->postalcode           = $page->parent->postalcode;
            $page->eventlocationname    = $page->parent->eventlocationname;
            $page->place                = $page->parent->place;
            $page->region               = $page->parent->region;
            $page->country              = $page->parent->country;
            $page->googlemap            = $page->parent->googlemap;
            //copy all fields field values from the parent template (end)

This ist the part where you can fill in all fields which you want to copy the values. It copies the values from the parent page to the child page. You can do this also with a foreach loop, but I dont  want to copy all field values so I write it manually for each field.

The multilanguage part:

foreach ($this->languages as $lang) {
                $lname    = $lang->id;
                $pageName = $page->title->getLanguageValue($lang);
                $page->set("name$lang", $pageName);
                if ($lang->isDefault())
                    continue;
                $page->set("status$lang", 1);//activate the multilanguage checkbox
            }

The multilanguage part is necessary if you have a multilanguage site, because it creates the path names for each language in the right language.

After that the multilanguage checkbox for the non default language should be checked to activate the page in this language.

Here are some screenshots:

1) Press the add button of the pagetable field

post-2257-0-60536000-1452583696_thumb.pn

2) A new child page will be created with prefilled values of the parent page

post-2257-0-77192300-1452583735_thumb.pn

3) Path names are in the right language and multilanguage checkbox is activated

post-2257-0-33851900-1452583781_thumb.pn

So this might be useful for others

Best regards

  • Like 5
Link to comment
Share on other sites

  • 3 months later...

Hi Jürgen,

I was looking for something like this and built something similar which is more generic I guess - but I haven't tested it with complex fields so far.

In my module it is possible to set specific templates which are able to "give" field values and specific fields which are able to receive values.

Then, any field of a newly added page adopts those values according to the settings.

https://processwire.com/talk/topic/13183-adopt-field-values-as-defaults-from-parents/

Maybe this might also be useful for you.

best wishes

Steffen

  • Like 3
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...