Jump to content

Allow new pages to be created from field - required fields in (new page's) template


sgt.blikey
 Share

Recommended Posts

Hello,

I have selected to allow new pages to be created from a (page) field, e.g. "sub-category".

The template of the pages that would be created contains a field (e.g. "category", also a page field) that is required to have a value.

Currently pages are created and the field "cateogry" receives no value.

When editing a page that contains this field ("sub-category") and selecting "Create New" I would like to be able to specify a value for the field "category" in addition to being able to specify the title of the page that is created.

I think I have to modify/extend the function "processInputAddPages" in the module "InputfieldPage.module" in order to achieve that.

Would anyone have any advice regarding this?

Many thanks,

Nic

Link to comment
Share on other sites

this is a common situation; you have a page field, and you create a new page by adding the options there on the ASM select, but you need some other things to be configured on that page, in your case the category).

concerning your first request - the title, that should be set by the option you have entered, as long as you have configured it that way; but if you need more options those can be adjusted on page save using a module;

here is an example which you would need to put into a module; for example, i always have a generic 'SiteUtilities' module where i can throw in all kinds of generic stuff without having to make multiple modules, and worry about installing them or keeping track of them;

<?php 

class SiteUtilities extends WireData implements Module {

    /**
    * Basic information about module
    */
    public static function getModuleInfo() {
        return array(
            'title' => 'Site Utilities',
            'summary' => 'Various utility functions',
            'href' => '',
            'version' => 1,
            'autoload' => true,
            'singular' => true
        );
    }

    public function init() {
        $this->pages->addHookAfter('save', $this, 'AssignSubCategory');
    }

    public function AssignSubCategory($event) {
        $page = $event->arguments[0];
        if($page->template != 'someTemplate') return; // the template where you want this to run
    
        $category = $page->category; // this would be your category field
        $subCategory = $pages->get("something"); // this would be the page that is the sub-category
        foreach($category as $c) {    
            if($c->template != 'category') continue;
            $c->setOutputFormatting(false);
            $c->sub_category->add($subCategory);        
            $c->save();
            $this->message("Sub-category {$subCategory->title} added to Category{$c->title} ");
        }
    }

} // end class

Edited by horst
little code cleaning
  • Like 2
Link to comment
Share on other sites

Hello Macrura,

Thank you for your reply. I think I understand:

  • Create a module, MyModule.module, and place under site/modules/
  • In the module define its basic information, particularly that it should autoload and be singular.
  • Specify that the actions of the function are carried out when the page is saved.
  • Have the main function check for a particular template and do its thing when the page is saved.

If I have that correct I shall have to go away and work through it...

Many thanks,

Nic

Link to comment
Share on other sites

Hi Macrura,

Okay, I have successfully created a module that prints the message "Ping!" when the page is saved. Awesome.

I feel like I have to describe the whole system in order that I can apply your advice in the correct way, but I don't expect you to have to solve my problem for me, if you see what I mean.

The page structure is:

  • 'artworks' (template 'things')
    • 'artwork-01' (template 'thing')
    • 'artwork-02' (template 'thing')
    • etc
  • 'categories' (template 'cats')
    • 'glass' (template 'cat')
    • 'sculpture' (template 'cat')
    • etc
  • 'techniques' (template 'subcats')
    • fused glass (template 'subcat')
    • cast glass (template 'subcat')
    • ceramic (template 'subcat')
    • etc

Template 'thing' has fields 'title', 'cat' and 'subcat'

Template 'cat' has field 'title'

Template 'subcat' has fields 'title' and 'cat'

Field 'cat' is a page field; the parent of selectable pages is 'categories'; the template of selectable pages is 'cat'

Field 'subcat' is a page field; the parent of selectable pages is 'techniques'; the template of selectable pages is 'subcat'

Both fields have Deference in API as set to 'Single page (Page) or empty page (NullPage) when none selected' (I don't fully understand the significance of the two single page options).

Here is will be a screen shot of an artwork page being edited. I have chosen a category for this artwork and want to create a new subcategory 'under' the category that has been selected, in this case 'glass'. (i.e. I can say $pages->find("template=subcat, cat=glass"); to get all the subcategories of the category 'glass'.)

Let's say I enter 'sand cast' in the text box for the title of the new subcategory. On Save I want the page 'sand cast' to be created under 'subcategories' and the category 'glass' entered as a value for the field 'category' on that page.

That is why I thought I should be looking at the module InputfieldPage.module, because on Save that (I think) is the module that contains the function that adds pages.

Many thanks,

Nic

post-2173-0-38904600-1427150565_thumb.jp

Link to comment
Share on other sites

should be really simple; this is untested, but this is basically the idea:

<?php 

class SiteUtilities extends WireData implements Module {

    /**
    * Basic information about module
    */
    public static function getModuleInfo() {
        return array(
            'title' => 'Site Utilities',
            'summary' => 'Various utility functions',
            'href' => '',
            'version' => 1,
            'autoload' => true,
            'singular' => true
        );
    }

    public function init() {
        $this->pages->addHookAfter('save', $this, 'AssignCatToSubcat');
    }

    public function AssignCatToSubcat($event) {
        
        $page = $event->arguments[0];
        if($page->template != 'thing') return;
    
            $category       = $page->cat; 
            $subCategory    = $page->subcat;

            $subCategory->setOutputFormatting(false);
            $subCategory->cat = $category;
            $subCategory->save();
    }

} 
  • Like 1
Link to comment
Share on other sites

Hi Macrura,

That really helped me. There are a couple of problems that I have to solve, which are:

  • When exactly the module is active - it needs to be more specific than if the page template is 'thing' because it executes when the thing is being created, when none of the fields are ready.
  • What to do if $page->cat is null - the subcat page should not be created if no cat is set.

I think I'll be back here if I'm unable to figure those problems out.

Many thanks for your help,

I am now reading http://processwire.com/api/hooks/

Nic

Link to comment
Share on other sites

yes, you'll have to do some additional checking in the function - i usually would add additional conditions, something like this:

<?php 

class SiteUtilities extends WireData implements Module {

    /**
    * Basic information about module
    */
    public static function getModuleInfo() {
        return array(
            'title' => 'Site Utilities',
            'summary' => 'Various utility functions',
            'href' => '',
            'version' => 1,
            'autoload' => true,
            'singular' => true
        );
    }

    public function init() {
        $this->pages->addHookAfter('save', $this, 'AssignCatToSubcat');
    }

    public function AssignCatToSubcat($event) {
        
        $page = $event->arguments[0];
        
        if($page->template != 'thing') return;
        if($page->cat == '') return;
        if($page->subcat == '') return;

        $category       = $page->cat; 
        $subCategory    = $page->subcat;

        $subCategory->setOutputFormatting(false);
        $subCategory->cat = $category;
        $subCategory->save();
    }

} 
Link to comment
Share on other sites

Hi Macrura,

Thought I'd let you know that I ran away and hid from this. I restructured the page tree and made some compromises. Doing so released me from having to pursue this, which had me punching way above my weight.

Many thanks for your help,

Nic

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