Limiting quantities of subpages in blueprints

Is there a way in blueprints to limit how many subpages of each type a page can have?

I know how to limit which templates can be used as subpages, and I know how to limit the total number of subpages, but is there a way to put a maximum quantity on each individual allowed subpage template?

For example, if a page can have pageA, pageB, or pageC as subpages, is there a way to set it so that there can only be one of each subpage? So, if pageB already exists, you can add pageA or pageC but you cannot add a second pageB?

Thanks!

No, that’s not possible via a blueprint settings.

You can either use a hook or maybe a model (downside: user can try to add a page and only after trying get the error message), or even a custom page creation dialog with its own logic.

You can’t do it from the blueprint as @pixelijn already said. You can achieve it with a page.create:before hook though.

<?php

return [
  // other settings...
  'hooks' => [
    'page.create:before' => function ($page, $input) {
      $template = $page->intendedTemplate()->name();
      $siblings = $page->parentModel()
        ->childrenAndDrafts()
        ->template($template);
      
      $templates = [
        'about' => 1,
        'articles' => 3,
        'modules' => 1,
        // other templates and limits
      ];
      $limit = $templates[$template] ?? null;

      if ($limit && $siblings->count() >= $limit) {
        throw new Exception("You can not add more than {$limit} '{$page->blueprint()->title()}' pages.");
      }
    },
  ],
];

Replace the $templates variable with the template names and limits that suit your needs.

Throwing an exception from a *.*:before hook will prevent the model from being created and display the exception message to the user.

1 Like