BitPoet

Template Once Per Parent

Recommended Posts

So I stumbled over the request to allow limiting templates to be used only once under every parent page in this thread

and found that this would actually come in handy (also in a site I've built).

The code can be found on github and soon also in the module repo.

After installation, you'll find a new checkbox "Only once per parent" in the family tab when editing a template.

topp.png.bab0e1e38e42f963d640a398b7c0fc53.png

Edited by BitPoet
Added link to module repository
  • Like 5

Share this post


Link to post
Share on other sites

Hi @BitPoet - love the response time on this :)

Just a oversight I know, but can I make a suggestion that you please change:

$parent->children("template={$template}")->count() > 0;

to:

$parent->count("template={$template}") > 0;

The execution time on a parent with a lot of children is going to be a problem otherwise.

  • Like 2

Share this post


Link to post
Share on other sites
6 minutes ago, adrian said:

The execution time on a parent with a lot of children is going to be a problem otherwise.

That was my initial thought too, but then I realised that this module by definition applies to situations where there is only zero or one child of that template under a parent. So that selector is at most going to return one page.

Share this post


Link to post
Share on other sites
1 minute ago, Robin S said:

That was my initial thought too, but then I realised that this module by definition applies to situations where there is only zero or one child of that template under a parent. So that selector is at most going to return one page.

Right - I guess I was just worried what would happen if someone applied that setting to an existing template on their site accidentally. Maybe unlikely and not a big deal, but is there any reason not to change to my version?

  • Like 1

Share this post


Link to post
Share on other sites

No reason at all, so 0.1.1 on github already has the change. Thanks for taking a look and pointing it out @adrian

:)

  • Like 2

Share this post


Link to post
Share on other sites

Hi, I remembered this module and now I needed it in a project. However, it seems that it cannot handle childrens with multiple type of templates. I would like to add 2 children pages per parent, "answer" and "review". When I add one, I cannot add the second one - so if I have added an "answer", I get "No templates allowed for adding new pages here." error when trying to add a "review". The parent template have both templates enabled as children templates.

Here is a quick fix (about line 40), of course please review if needed:

if($template->oncePerParent && count($parent->children("template={$template}")) > 0) {

instead of 

if($template->oncePerParent && $parent->count("template={$template}") > 0) {

 

Share this post


Link to post
Share on other sites

+1: this removes the "New" action if there is no room for more children. I haven't tested it thoroughly so may have issues.

        $this->addHookAfter('ProcessPageListActions::getActions', null, function(HookEvent $event) {

            $p = $event->arguments[0];
            $actions = $event->return;
            $numChildrenWithOncePerParent = $p->children()->count('template.oncePerParent=1');

            if($numChildrenWithOncePerParent > 0 && (count($p->template->childTemplates) <= $numChildrenWithOncePerParent)) {
                unset($actions['new']);
            }

            $event->return = $actions;
        });

 

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By kixe
      Email Verification
      This module provides functions to validate email adresses and hosts. The module generates a textfile for blacklisted mailhosts (trashmail), which will be always up to date.
      Download
      https://modules.processwire.com/modules/email-verification/
      API
      // get module $mailcheck = $modules->get('EmailVerification'); // return bool/ string - automatted update of blacklist file $mailcheck->blacklisted(email|domain) // return bool - validate a top level domain, checks against IANA list $mailcheck->validTLD(tld) // return array of punycoded TLDs - cyclic updated, data pulled from IANA $mailcheck->getTLDs(cycle=2592000) // return bool - checks syntax converts to punycode $mailcheck->validDomainName(domain); // return bool - checks punycode encoded syntax $mailcheck->validHostName(host); // return bool - checks syntax and accessibility $mailcheck->validHost(email|domain) // add a single value to blacklist $mailcheck->addToBlacklist(email|domain) USAGE
      $mailcheck = $modules->get('EmailCheck'); $email = 'susi@trashmail.com'; if($mailcheck->blacklisted($email)) echo 'Email Provider not allowed'; if(!$mailcheck->validHost($email)) echo 'Mailhost not available'; Example blacklist file:  blacklist.txt
       
    • By teppo
      Code Blocks Textformatter is a tiny Textformatter module I cooked up to add support for code blocks to text/textarea/RTE fields on some of the sites I work with. Unlike a full-blown Markdown Textformatter – which is something that we already have in the core – this module simply adds support for fenced and inline code blocks.
      The syntax is based on the GitHub code block documentation, so please refer to that for additional instructions. The README at GitHub also includes some basic examples. As with any Textformatter, in order to enable this one, install it and enable it via field settings. Note that there's no syntax highlighting built in (at least for the time being), so use a tool of your choice for that – personally I prefer Prism.js.
      Since this module doesn't use a Markdown tool behind the scenes, but rather some home baked regular expressions, there's always the possibility that I've missed something – but please let me know if you use this module and run into any issues. On the other hand this module should be relatively fast and unobtrusive, as there are no unnecessary bits of code to run 
      GitHub repository: https://github.com/teppokoivula/TextformatterCodeBlocks Modules directory: http://modules.processwire.com/modules/textformatter-code-blocks/
    • By theo
      Here is my first processwire module (beta).
      https://github.com/theo222/ImagePickerField
      A lot of "first times". First time Github, Markdown and of course PW Modules.
      So be gentle please.
      I hope it works.

    • By horst
      Image Animated GIF  v 2.0.2
       
      Module for PW 2.5 stable or newer, but is obsolete for PW Versions greater then 3.0.89 (... read more ...)
       
      This module helps with resizing and cropping of animated GIFs when using the GD-Library.
      The GD-Library does not support this.


      This module is completely based upon the fantastic work of

         László Zsidi (http://www.gifs.hu/, builts the initial classes)
         xurei (https://github.com/xurei/GIFDecoder_optimized, enhanced the classes)


      I have ported it to a PW module and it works with core imagefields, together with Pia and with CropImagefields that uses the new naming scheme since PW 2.5.
       
      ------------------------------------------------------------------------------------------------------------------------------------------
      read more in this post about the rewrite from version 1.0.0 to version 2.0.0
      ------------------------------------------------------------------------------------------------------------------------------------------
      You can find it in the modules directory: https://modules.processwire.com/modules/image-animated-gif/
      and on Github: https://github.com/horst-n/ImageAnimatedGif
      ------
      A preview can be found here