Jump to content

InputfieldPageAutocomplete - create new pages


SteveB
 Share

Recommended Posts

For a page field with InputfieldPageAutocomplete the option to allow new pages to be created from the field doesn't work. I assumed that's because I'm not following these rules:

1. Both a parent and template must be selected above.
2. The editing user must have access to create/publish these pages.
3. The label-field must be set to "title (default)".

Instead of doing it that way I use a custom selector (template=feature,sort=name). It works fine for selecting existing pages but I can't make new pages with it.

I don't want them all under a single parent because there are thousands of selectable options. I have a HookAfterPageSave module setup to automatically set the parent (for pages with "feature" template) to one of several folders based on the first letter of the title (folders named  "A" to "Z" and "Numbers"). That works fine when I create and save them via the API as part of a data import.

To satisfy that rule #1 above I tried setting parent to one of the folders just mentioned thinking that it wouldn't disturb anything since HookAfterPageSave is going to save new pages to the right parent anyway and the my custom selector will be used to find the items.

This fooling around with settings did not allow me to add new pages.

Any suggestions?

Link to comment
Share on other sites

If I put code in InputfieldPage.module ___renderAddable function to conditionally set $this->parent_id and $this->template_id the autocomplete box will prompt me to "Hit enter to add a new item." The new value then appears in the list that's part of the UI (the drag and drop bars) but when I save the page I see that no new item has been made. No errors either.

Later...

I think I can deal with this via hooks but I could use some help with the hooks.

I'd like to conditionally change some properties of $this in InputfieldPage.module methods ___renderAddable and ___processInputAddPages

I setup a module file like this:

class HookInputFieldPageAutocomplete extends WireData implements Module {

	public static function getModuleInfo() {

		return array(
			'title' => 'HookInputFieldPageAutocomplete', 
			'version' => 1, 
			'summary' => 'Modify autocomplete to add own processes.',
			'singular' => true, // Limit the module to a single instance
			'autoload' => true, // Load the module with every call to ProcessWire 
			);
	}

	public function init() {
		// init() is called when the module is loaded.
		$this->addHookBefore('InputfieldPage::renderAddable', $this, 'beforeRenderAddable'); 
		$this->addHookBefore('InputfieldPage::processInputAddPages', $this, 'beforeProcessInputAddPages'); 
	}
 
       public function beforeRenderAddable() {
            if($this->template_id == 60){    
                $this->parent_id = 13043;    
            }
        }
    
        public function beforeProcessInputAddPages() {
            if($this->template_id == 60){    
                $this->parent_id = 13043;    
            }
        }
}

Questions:

Any obvious beginner type errors?

Is the hooked function's $this also $this in the context of the functions beforeRenderAddable and beforeProcessInputAddPages?

What I'm trying to do is evaluate that conditional before either of those two InputfieldPage methods is run. I could just put a modified version of the module in my sites/modules folder and be done with it but this seemed like an opportunity to try something new with hooks

Thanks

Link to comment
Share on other sites

sometimes i use 2 fields in this type of situation, one i'll call an "add item utility" and the other is the regular page select;

so i can add the items in the 'utility' field and then select them in the page select;

it was the only way i could come up with to achieve a similar functionality as you have described, e.g the utility field adds the item only, and then it can be selected from the other field..

  • Like 1
Link to comment
Share on other sites

Interesting idea! Splitting up creation and selection makes things clearer to the user too.

I think I'll finish a modified InputfieldPage module working I've been working on and then try your suggestion. In this usage it would be good to have adding new options be a bit more deliberate. Thanks.

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