Jump to content

Customize "add new page" screen?


dragan
 Share

Recommended Posts

A question just popped up in our office:

Is it possible to customize the very first screen when you choose "add new page"?

Would it be possible (with a hook maybe?) to add another input field (besides title + name)?

Or maybe even use another mandatory field than title? Of course, then I would have to take care myself of the name-creation (sanitize, plus making sure it's unique). Not sure I'd want that, really, for what is ultimately just a "nice-to-have" feature.

Link to comment
Share on other sites

I've once tried to modify adding of pages (I think it was related to the 1-step-adding; something like i added a link to add a new page and then wanted to modify some fields of that page) and found it was easier to create a custom endpoint for that process and redirect:

public function executeAddmypage() {
  $page = new Page();
  $page->parent = 123;
  $page->template = 'mytemplate';
  $page->myfields = ...
  $page->save();
  wire('session')->redirect(wire('pages')->get(123)->editUrl;
}

thanks for the idea, I'll add such an example with more detailed explanations to my guest blog post coming on friday :)

...if others don't come up with a better idea :) 

Link to comment
Share on other sites

Looking at the hookable methods in ProcessPageAdd it looks like this would be possible. But what are the circumstances where you would want to do this? Remember that no template for the page has been selected at this point (unless the page is being added under a parent that allows only a single child template) so in general you don't know if a given field is going to be present in the newly saved page.

If you can explain a bit more about what you are wanting to do I'll post some example code.

  • Like 1
Link to comment
Share on other sites

Thanks for the feedbacks so far. 

@Robin S you're correct - if I would use this, it's always just under one certain parent, and only one certain template is allowed. I already hide the template select dropdown for the admins on this screen, since it only adds confusion. The use-case here is: People quickly create pages, keep them unpublished, and get back later to fill in all necessary fields. The only other text field on the "add new page" screen would be some internal project code (which is unique; a cross-reference to our CRM/accounting software). That way, they can first check if the exact same page already exists. Of course, I was thinking about other methods, e.g. use batcher. 

Link to comment
Share on other sites

@dragan, you can add the inputfield like this:

$wire->addHookAfter('ProcessPageAdd::buildForm', function(HookEvent $event) {
    $form = $event->return;
    // If adding page under particular parent
    if($this->input->parent_id === '1234') {
        // Add inputfield to form after existing title field
        // If you name the inputfield the same as the field you want to populate
        // then the entered value will be saved automatically
        $f = $this->modules->InputfieldText;
        $f->name = 'vertec_code';
        $f->label = 'Vertec-Code';
        $f->required = true;
        $f->attr('required', 1); // use HTML required attribute too
        $title_field = $form->getChildByName('title');
        $form->insertAfter($f, $title_field);
        $event->return = $form;
    }
});

Because of this section in ProcessPageAdd::processInput, if you name the inputfield the same as the corresponding field in the template of the new page then the entered value will automatically be saved to the page.

Edited by Robin S
Made inputfield "required"
  • Like 6
  • Thanks 2
Link to comment
Share on other sites

I have a question though:

I have a template override for the title field in place. This works, but only in page-edit mode.

i.e. I see my label + description when in page-edit view.

But on this first "add new page" screen I only see the default "Title" label, and no description.

Can I somehow force PW to use my template override  on this screen? Or is it necessary for PW to first save the page, to apply this setting? (makes sense)

Can I add two lines to the above code and "inject" my title-label & description?

Edit: 

OMG, I use PW now what? since 5 years, or more? I should know better by now that it's as easy as this:

$title_field              = $form->getChildByName('title');
$title_field->label       = 'my custom label';
$title_field->description = 'my custom description / help-text / instructions / examples';
$form->insertAfter($f, $title_field);

sorry for even asking...

Edited by dragan
my own stupidity
  • Like 2
Link to comment
Share on other sites

7 hours ago, dragan said:

 


$title_field              = $form->getChildByName('title');
$title_field->label       = 'my custom label';
$title_field->description = 'my custom description / help-text / instructions / examples';
$form->insertAfter($f, $title_field);

Are you sure this is necessary? When adding a new page under a parent that allows only a single template for children, I find that any changes made to the title field in template context (e.g. changing label or description) are automatically applied.

Link to comment
Share on other sites

I think so.

At least in my setup: I use a generic "overview" template all over the place, that only acts as parent page (to group / store children). They're not even used to show anything in the frontend. Of course I know I could create a special template just for this use case, and have the in-built control with "only allowed templates for children".

but afaik, there is no way I can restrict children templates from the page-tree, only on a per-template-basis. Well, unless there's a module that allows this...

 

Link to comment
Share on other sites

26 minutes ago, dragan said:

but afaik, there is no way I can restrict children templates from the page-tree, only on a per-template-basis. Well, unless there's a module that allows this...

It just so happens that there is... Templates: Child Pages :-)

You could use this in conjunction with the hook below, which will use the template context for title label, description and notes if the number of allowed templates has been restricted to one:

$wire->addHookAfter('ProcessPageAdd::buildForm', function(HookEvent $event) {
    $form = $event->return;
    $template_field = $form->getChildByName('template');
    if($template_field->type == 'hidden') {
        // Only one template is allowed for this new page
        // Get the template
        $template = $this->templates->get($template_field->value);
        // Get the title field in the context of this template
        $title = $template->fieldgroup->getFieldContext('title');
        if($title) {
            // Set the properties of the 'title' inputfield
            $title_field = $form->getChildByName('title');
            $title_field->label = $title->label;
            $title_field->description = $title->description;
            $title_field->notes = $title->notes;
        }
    }
    $event->return = $form;
});

 

Edited by Robin S
Whoops, accidentally delete most of my post. Fixed.
  • Like 3
Link to comment
Share on other sites

1 hour ago, dragan said:

Can you pls explain why this line is necessary?


if($template_field->type == 'hidden') {

This is a test for if only a single template may be used for the new page. PW uses a hidden field for template in this case.

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