Jump to content

Disable "new page" functionality if parent page has insufficient data


Recommended Posts

Posted

Hello dear forum,

I'm building the backend of a kind of magazine. The magazine has different contributors.
Each contributor has it's own tree in the page tree, like this:

home/user-articles/contributor-a/

and should be able to post articles as children to contributor-a.
On the contributor-a page (template called "article-list") there needs to be a list of dates. These dates (a repeater in "article list") are used as select options in the article the user is going to post. So far so good.

However, if the user tries to create a new article, and there haven't been any dates posted to the future parent yet, I get an error :
"TypeError
ProcessWire\InputfieldPage::getPageLabel(): Argument #1 ($page) must be of type ProcessWire\Page, string given, called in /var/www/html/wire/modules/Inputfield/InputfieldPage/InputfieldPage.module on line 700"
wich only occurs when there's no date(s) posted to the parent yet.

Now I'm thinking about only allowing adding new children to that "article-list" template when at least one date is available, and am stumped as to how to solve this.
I basically need to disable the "new" button in the tree view - or basically anwhere it might show up - and post a message explaining what's up. Pretty sure I need to hook into ready.php (or admin.php) but other than that I'm struggling..

If anyone has any pointers (or just some skeleton code) that would be great, thanks in advance!
 

Posted

Hook after Page::addable. $event->object will be the page to add a child to. Untested example code written off the top of my head:

<?php namespace ProcessWire;

// For ready.php

wire()->addHookAfter('Page::addable', function(HookEvent $event) {

	$thisPage = $event->object;
	
  	// Exit early if already disallowed
    if(! $event->return)
      return;
  
  	// Only apply to pages with template article-list
	if($thisPage->template->name !== 'article-list')
		return;
	
  	// Now check the fields on the page whether they allow
  	// adding children. Adapt the field name to your use case.
	if($thisPage->dateslistrepeater->count() == 0) {
		$event->return = false;
	}

});

This should disable the Add / Add Children button. You'll probably have to save the page after adding a repeater item before you can add children from the page editor.

  • Like 1
Posted

Ty!

I've had it working like this (skeleton code, in ready.php):
 

$wire->addHookAfter('Page::addable', function(HookEvent $event) {

    $parent = $event->object; 
    $tplArg = $event->arguments(0);
    $tplName = $tplArg instanceof Template ? $tplArg->name : (string) $tplArg;

    if($parent->template->name !== 'user-articles') return;

    $rep = $parent->get('repeater_publish_dates');
    $hasDates = ($rep && count($rep) > 0);

    if(!$hasDates) {
        $event->return = false;
    }

});

Pretty much 1:1 what you wrote.

  • Like 1

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.
×
×
  • Create New...