Jump to content

Hook when adding a new page?


FlorianA
 Share

Recommended Posts

Hi all,

I'd like to have a link in my front-end to add a new page (urls('admin')."page/add/?parent_id=$parentId"). I can't use front-end editing since I'd like to use the drag-and-drop area for uploading files. On the other hand, the back-end workflow with its two steps, defining title and url, then entering the rest, might be a bit confusing for end users. Therefore I'd like to display a kind of help message on the "New Page" page. What is the easiest way to do this? Is there a corresponding hook (couldn't find one)?

Link to comment
Share on other sites

Hi Florian,

I'm not sure I completely understand what you want to achieve, seems like there are different ways to achieve what you want. Could you describe it in steps?

As for displaying a message on the new page form, you can hook to ProcessPageAdd::buildForm. Here is an example.

// Hook is in /site/ready.php (or /site/templates/admin.php)

$this->addHookAfter('ProcessPageAdd::buildForm', function ($event) {
	// Retrieve the form
	$form = $event->return;

	// Create an inputfield markup to display some html.
	$field = $this->wire()->modules->get('InputfieldMarkup');
	$field->name = 'messageToUser';
	$field->value = "<p>Your message or anything else goes here!</p>";

	// Use "$form->add()" if you want your message at the end of the form (after button)
	$form->prepend($field);

	// Populate back argument
	$event->return = $form;
});
Link to comment
Share on other sites

You could also add a simple URL hook that creates the page and then redirects to its page editor:

<?php
// site/ready.php
// get the parent page
$parent = $page->get("/your/parent/page");
// add hook if the parent is editable
// adjust access control to your needs
if($parent->editable()) {
  // add hook to create page
  $wire->addHookAfter("/createpage", function($event) use($parent) {
    $p = new Page();
    $p->parent = $parent;
    $p->template = 'your-child-template';
    $p->save();
    $event->session->redirect($p->editUrl());
  });
}

And on the frontend:

<a href="/createpage">Create new Page</a>

 

  • Like 1
Link to comment
Share on other sites

Thanks for your replies.

I've already tried to create the page in advance and redirect the user to the edit URL. The drawback is that the page needs to get a dummy title that has to be overwritten by the user.

@Studio Lambelet, your hook does work, but is it also possible to retrieve the template of the parent page? This would be important for me in order to get a context sensitive message.

Maybe I'll try a third solution with two links in the frontend:

  • "Quick upload": User can specify a page title and file to be uploaded in a small form. Page will created without showing backend.
  • "Advanced upload": Regular call of the "new page" workflow which allows to specify more than one file and some more fields.
Link to comment
Share on other sites

Regarding the dummy title, I did not test this code, but based on @bernhard reply, I think this should work:

// ... Rest of the code before
$p = new Page();
$p->parent = $parent;
$p->template = 'your-child-template';
$p->title = 'your dummy title';
$p->save();
$event->session->redirect($p->editUrl());
// ... Rest of the code after

As for the parent page, in the admin Processwire use a GET input to pass the page id ("?parent_id=X).

// Hook is in /site/ready.php (or /site/templates/admin.php)

$this->addHookAfter('ProcessPageAdd::buildForm', function ($event) {
	// Retrieve GET input "parent_id"
	$parentIdInput = $this->wire('input')->get['parent_id'];
	// Sanitize GET input
	$parentId = $this->wire('sanitizer')->int($parentIdInput);
	// Get page
	$parentPage = $this->wire('pages')->get($parentId);

	if ($parentPage->id) {
		// Retrieve the form
		$form = $event->return;

		// Create an inputfield markup to display some html.
		$field = $this->wire()->modules->get('InputfieldMarkup');
		$field->name = 'messageToUser';
		$field->value = "<p>The parent page title is {$parentPage->title}.</p>";

		// Use "$form->add()" if you want your message at the end of the form (after button)
		$form->prepend($field);

		// Populate back argument
		$event->return = $form;
	}
});

 

  • Like 1
Link to comment
Share on other sites

12 hours ago, FlorianA said:

The drawback is that the page needs to get a dummy title that has to be overwritten by the user.

What do you mean? Is this how it should be? Or is this what you think has to be? If you want it to have a dummy title, @Studio Lambelet has shown the solution. If you think it must be like this: No, you can leave the title empty and the user has to fill the field on the first edit.

12 hours ago, FlorianA said:

"Quick upload": User can specify a page title and file to be uploaded in a small form. Page will created without showing backend.

You can even do this with my technique. Simply add those fields to the redirect url:

$event->session->redirect($p->editUrl()."?fields=title,your_upload_field");

 

12 hours ago, FlorianA said:

your hook does work, but is it also possible to retrieve the template of the parent page? This would be important for me in order to get a context sensitive message.

<?php
$wire->addHookAfter("/createpage/{parentID}", function($event) use($parent) {
  // access control
  $parent = $event->pages->get((int)$event->arguments('parentID'));
  if(!$parent->editable()) return;
  
  // create new page
  $p = new Page();
  $p->parent = $parent;
  $p->template = 'your-child-template';
  if($parent->template == 'your-parent-a') $p->title = 'Dummy text for parent A';
  elseif($parent->template == 'your-parent-b') $p->title = 'Dummy text for parent B';
  $p->save();
  
  // redirect to page editor
  $event->session->redirect($p->editUrl()."?fields=title,your_upload_field");
});

 

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