Jump to content

Get number of pages in module


Vayu Robins
 Share

Recommended Posts

Hi. I have a weird problem.

I have a form on a page built with FormBuilder. When a user submits this form, I use this code to get the number of pages like this from a module:

if( ! $page->id && $page->is( Page::statusUnpublished ) ) {

$references_parent = wire( 'pages' )->get('/case-references/' );
$num = $references_parent->count();

}

This code returns nothing wire( 'pages' )->get('/case-references/' ); It's as if it can't find anything.

I am really stuck on this one. Hope someone has an idea. :-)

Link to comment
Share on other sites

The method get() returns only one page. Your code is looking for a page with the path /case-references/. If this path doesn’t exist on your site, you will get a NullPage. You can check if you got a NullPage by testing for $references_parent->id === 0 or $references_parent instanceof NullPage. Because you only get a single page from get(), your variable $num will always be 0, as page objects don’t have a count.

To find multiple pages, you should use wire('pages')->find(). Unfortunately it is unclear which pages you want exactly. I’m guessing you want all the children of /case-references/. In that case, try wire('pages')->find('parent=/case-references/'). You may also want to specify the template of the pages you’re looking for.

Also in your first line you are testing whether $page->id is false AND $page is unpublished. That seems like an unlikely combination, because the NullPage is usually published.

  • Like 1
Link to comment
Share on other sites

Hi, thanks for the quick response. However, it's not working either way. I have already tested all these methods and they all return the correct answer when I print it in a page template, but not from my module.

Here's what I am trying to do. When a user submits the form, two new pages are created. One case and one reference. The case must have a sequential reference number, which I get from creating a new reference page and count the total of.

In the module I am calling the code from the Pages::saveReady hook. It was working up until recently, so something must have happened somewhere. I have a parent page named "case-references". On this parent page I am creating a new child page. The child page must be named the number of total child pages. My problem is that I can't get that total number.

wire('pages')->count("parent=/case-references/");
wire('pages')->get("/case-references/")->count();
wire('pages')->get("/case-references/")->children->count();
wire('pages')->find("template=case-reference")->count();

I wonder if another module I have installed lately, could interfere or could it be something in Processwire 2.6.1 that changed, because a few days ago, it was working perfectly.

Here is the whole code:

public function init() {
	$this->addHookAfter( 'Pages::saveReady', $this, 'createReferenceCase' );
}
public function createReferenceCase( $event ) {
	// Get the soon to be saved page object from the given event
	$page = $event->arguments[0];

	if( $page->template == "case" && !$page->isTrash() ){

		// When form is submitted, create page and update reference input field.
		if( ! $page->id && $page->is( Page::statusUnpublished ) ) {
			// Get references parent page
			$references_parent = wire( 'pages' )->get('/case-references/' );
			
			// Create new reference page
			$r = new Page(); // create new page object
			$r->template = 'case-reference'; // set template
			$r->parent = $references_parent; // set the parent 
			$reference_num = wire('pages')->count("parent=/case-references/") + 1;
			$r->title = '#'.$reference_num; // set page title (not neccessary but recommended)
			
			// populate fields
			$r->reference = wire( 'sanitizer' )->fieldName( $reference_num ); // populate a single
			$r->save();
			
			// Add reference id to case
			$reference_name = date('Y') . $reference_num;
			$page->name = wire( 'sanitizer' )->name( $reference_name );
			$page->reference = '#'. wire( 'sanitizer' )->fieldName( $reference_name );
		}
	}
}
Link to comment
Share on other sites

Since you already have $references_parent in a variable, why not use $references_parent->numChildren(true)? It’ll give you the total number of children including pages that are hidden/unpublished/etc. Set to false to get only visibles.

Another idea would be to use $r->sort. Even with manual sorting that should always be the highest number immediately after adding the page.

Be aware that both these methods will lead to duplicate numbers if you decide to delete one of the existing pages! You should probably keep a record of the highest number assigned somewhere central, if you need these reference numbers to be unique.

Link to comment
Share on other sites

Not much help, but I just tested your code (slightly modified so I can just paste into admin.php) and it is working fine here in the latest dev version of PW, so I think you must have something else going on. Can you test on a clean dev install to see if you can help to narrow down the issue?

Link to comment
Share on other sites

Okay, I finally found the cause of it all. During one of the last few days, I must have changed the Access level for the child pages, which in turn prevented me from searching them, when not logged in. This works.

wire('pages')->count("parent=/case-references/, check_access=0");

Thank you everyone for helping me! :P

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