Jump to content

Counting children that are viewable


MadHatter
 Share

Recommended Posts

Hi, I've been trying to return the number of children that a page has that are viewable but I'm just getting back the total number of pages. The pages aren't viewable because there is no related template file. $page->numChildren() does not work (both true and false returns the same value).

The current method we've been using it to run a command in the _init.php file that checks for pages that use that template and set them to hidden. I'm concerned there will be performance penalties for this.

Link to comment
Share on other sites

There may be nothing to do about this. The database cannot know that your page is not viewable because of the missing template file. Therefore you need to check viewability at runtime, no matter how you'd do that. About your performance concern. You could cache the viewability of pages in an global field and check again only after a certain timeframe. 

Link to comment
Share on other sites

That seems like there may end up being issues with data going out of sync. A sensible solution would be to add an option to the template settings to hide pages of that type from counts and searches.

Link to comment
Share on other sites

That seems like there may end up being issues with data going out of sync.

True, but depending on the use-case this doesn't have to be an issue. 

A sensible solution would be to add an option to the template settings to hide pages of that type from counts and searches.

// _init.php
// do not overwrite sys variables $templates, $template
$filelessTemplates = new TemplatesArray();
foreach($templates as $templ){
  if(!is_file($templ->filename)) $filelessTemplates->add($templ);
}

// elsewhere
$pages->count("…, template!=$filelessTemplates");
Link to comment
Share on other sites

related to the _init.php snippet you could use this while it has no affect in the frontend:

The current method we've been using it to run a command in the _init.php file that checks for pages that use that template and set them to hidden. I'm concerned there will be performance penalties for this.

may a solution could be to add this code from ryan here to set pages with specific template to hidden:

$pages->addHook('saveReady', null, 'makePageHidden'); 
function makePageHidden(HookEvent $event) {
  $page = $event->arguments(0); 
  if($page->template != 'category-site') return; // replace 'category-site' with your template name
  if(!$page->is(Page::statusHidden)) $page->addStatus(Page::statusHidden); 
} 

add this in your /site/templates/admin.php, after the opening <?php tag

with this Pages::saveReady Hook

it processes only before finishing saving a page...so just for new pages working for existing you should go to setup a API snippet with all needed pages and a foreach loop to edit all of the existing pages to setup up hidden status...

With this function it is equal what a user set to status it would be always hidden after saving.

regards mr-fan

  • Like 1
Link to comment
Share on other sites

Thanks mr-fan,

I'll give this a go, I was thinking that some sort of hook would be the safest options, but I wasn't sure where the best place to implement it would be.

EDIT: Works perfectly, thanks!

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