Subpage builder - can't make it works

Hi everyone,

I’m building a page structured by full-width lines. Each line is composed of three columns.
I want that when the user create a new line in the panel, it comes with three subpages (wich contents are filling the columns).

I found the guide concerning the subpage builder. I followed each step but my website doesn’t display anything now, the front neither the panel.

Here is my php :

<?php foreach ($page->children() as $line): ?>
<section class="line">
    <?php foreach ($line->children() as $block): ?>
    <div class="line__container">
        <img src="<?= $block->image()->url() ?>" alt="" class="line__container__img">
    </div>
    <?php endforeach ?>

</section>
<?php endforeach ?>

<footer class="footer">
    <nav>
        <ul class="menu">
            <li class="menu__item">
                <a href="#">
                    <h1>Régulier</h1>
                </a>
            </li>
        </ul>
    </nav>
</footer>

First, in my blueprint line.yml, I added :

 subpage_builder:
    - title: Bloc 1
        uid: bloc
        template: bloc
        num: 1
    - title: Bloc 2
        uid: bloc
        num: 2
        template: bloc
    - title: Bloc 3
        uid: bloc
        num: 3
        template: bloc

In my config.php, I added the hook :

<?php

return = [
    'debug' => true,
    'hooks' => [
        'page.create:after' => function ($page) {
            buildPageTree($page);
        }
    ],
]

I created the files (and the parent folders) /site/plugins/treebuilder/index.php, where I added :

<?php

function buildPageTree($page) {

    // get the subpage_builder definition from the blueprint
    $builder = $page->blueprint()->subpage_builder();

    // if any is set…
    if (empty($builder) === false) {

        // …loop through each page to be built
        foreach($builder as $build) {

            // check if all necessary fields have been defined
            // in subpage_builder
            $missing = A::missing($build, ['title', 'template', 'uid']);

            // if any is missing, skip
            if (empty($missing) === false) {
                continue;
            }

            try {
                // the parent itself is created as a draft
                $subPage = $page->createChild([
                    'content'  => [
                        'title' => $build['title']
                    ],
                    'slug'     => $build['uid'],
                    'template' => $build['template'],
                ]);
            } catch (Exception $error) {
                throw new Exception($error);
            }

            // publish the subpages, so that they are published
            // when the parent is published
            if ($subPage) {

                // call the function recursively
                buildPageTree($subPage);

                // publish subpages and sort
                $subPage->publish();

                if (isset($build['num']) === true) {
                    $subPage->sort($build['num']);
                }
            }
        }
    }
}

Any idea ?

Thanks a lot for your help.

P.S. I tried to delete all the files and code I added but my website still doesn’t work anymore.

Your subpage builder contains 3 subpages with the same UID, that is not possible. Each UID must be unique.

Thx @texnixe, I rectified.

subpage_builder:
    - title: Bloc 1
        uid: bloc-left
        template: bloc
        num: 1
    - title: Bloc 2
        uid: bloc-center
        num: 2
        template: bloc
    - title: Bloc 3
        uid: bloc-right
        num: 3
        template: bloc

The site still doesn’t display anything, every pages are totally blank. Same for the panel.

Are the subpages created when you create a new page with the line template?

I can’t create a new line because the website doesn’t display anything. It is totally blank. Even the panel.

Is debugging enabled?

yes

It seems that it doesn’t comes from my localhost because my other websites are running normally.

Do you have a php logs file where you could check if you have a PHP error somewhere?

No, I don’t even know what a php logs file is.

What is your local dev environment?

I use MAMP and Brackets

In your MAMP folder, there is a /logs folder and inside it, a file called php_error.log

Thx, here the only error corresponding to today’s date :

[08-Apr-2020 10:38:33 UTC] PHP Parse error: syntax error, unexpected ‘=’, expecting ‘;’ in /Applications/MAMP/htdocs/rythme/site/config/config.php on line 3

Indeed there was an unexpected “=”. I deleted it and the website is displayed again !

And the subpage builder works !

Thx again, and again @texnixe

1 Like