Change page content in hook

I’d like to adjust page content inside the page.create:before hook. The docs are a bit mysterious about how to use this hook:

return [
    'hooks' => [
        'page.create:before' => function ($page, $input) {
            // your code goes here
        }
    ]
]

$page is the new page object with all content set.
$input seems to be the array version of this page object.

My questions are:

  1. Which of the two parameters am I supposed to change when I’d like to set a content value?
  2. What am I supposed to return?
  3. Am I actually supposed to return anything in the hook?
  1. You can’t change the page content in page.create:before hook, you should use page.create:after to modify page content. before hooks is often used for validation purposes.

  2. You dont need any return except Exception to prevent creation like that:

     return [
         'hooks' => [
             'page.create:before' => function ($page, $input) {
                 if($condition === false) {
                     throw new Exception("This page cannot be created.");
                 }
             }
         ]
     ]
    
  3. Related two

2 Likes

Thanks @ahmetbora!

1 Like