Checking if a user can view a child page

Permalink
Hi,

I have the following code

<?php
$page = \Page::getCurrentPage();
$children = $page->getCollectionChildren();
$counter = 1;
foreach($children as $child){
    if($counter == 4){
        echo "<div class='row'>";
    }
    if($counter == 8){
        echo "<div class='row'>";
    }
    $childName = $child->getCollectionName();
    echo "<div class='col-md-3'><div class='well'>";
    echo $childName;
    echo "</div></div>";


I am trying to programatically create a list of available pages to the user with a link attached to the page.

How do I check whether the current user has access to view the current child page that is in the loop, and how would I attach a link to that child page to direct them to the page?

I have tried
$child->canViewPage();
but it did not seem to do anything but cause an error.

 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi dkw15,

You can try this. I wrapped the changes in "CHANGES" and "END CHANGES".
<?php
$page = \Page::getCurrentPage();
$children = $page->getCollectionChildren();
$counter = 1;
foreach ($children as $child) {
    if ($counter == 4) {
        echo "<div class='row'>";
    }
    if ($counter == 8) {
        echo "<div class='row'>";
    }
    $childName = $child->getCollectionName();
    // CHANGES
    $childLink = $child->getCollectionLink();
    // END CHANGES

Each child is a Page object that you can check the permissions of.