Duplicated page entries in structure field are not showing

Hello, I have a structure field where the client is able to create an entry and choose a page. Additionally, they can choose how the page is displayed (in a slider, static etc) based on the site settings.

My issue comes when I want to have two or more same pages (duplicated) to be showed in different styles. No matter how many duplicates I have in the structure, only the latest of the duplicated will show.

Is this standard behaviour? Is there a way to tweak this to allow to show all these without needing to duplicate the pages in question?

Thanks in advance.

It would be helpful if you post your code, thanks!

The code has evolved so much that I think will be confusing to share the full code, but I was curious if that’s standard behaviour or by default it should show all items no matter if they’re duplicates of the same page.

Essentially I am doing:

$items = $page->projects()->toStructure();
foreach ($items as $item): ?>
$item->project()->toPage())

Not sure if that’s enough to add some clarity. Happy to try to expand this more without it becoming too confusing.

Thanks.

If I understand correctly, in your structure you have a field called project which is a pages field. And the structure field can have the same page selected in multiple of these project fields.

But I don’t see why the same page should not be displayed multiple times with the code you show above.

Yes, that’s correct. I found out this morning my local version of the site displays the duplicated items normally, but the one in production doesn’t. It’s really strange. I will keep digging to see what useful information I can find. Thanks!

I figured out where the issue is.

I have this function in config.php that re-indexes the items url’s from the original page to the duplicated one once duplicating a page. That way, when duplicating a page, the links from the newly duplicated structure page are linked to the new page instead of the original one.

I realised the problem happens on these duplicated pages only, not the original ones. Any idea why this could be happen?

    // Update structure field on duplicate
    'page.duplicate:after' => function ($duplicatePage, $originalPage) {
        $projects = $duplicatePage->projects()->toStructure()->toArray(); // Get the projects structure field

        foreach ($projects as &$project) {

            if (!empty($project['project']) && is_array($project['project'])) {
                $originalProjectName = explode('/', $project['project'][0]);
                $originalProjectNameSlug = end($originalProjectName);
                $duplicatedProjectPage = $duplicatePage->children()->findBy('slug', $originalProjectNameSlug); // Find corresponding page in the duplicated
                $project['project'] = [$duplicatedProjectPage->id()]; // Assign the new project page object
            }

            if (!empty($project['selection']) && is_array($project['selection'])) {
                $originalProjectNames = $project['selection'];
                $duplicatedProjectPages = [];

                foreach ($originalProjectNames as $originalProjectName) {
                    $originalProjectNameSlug = explode('/', $originalProjectName);
                    $originalProjectNameSlug = end($originalProjectNameSlug);
                    $duplicatedProjectPage = $duplicatePage->children()->findBy('slug', $originalProjectNameSlug); // Find corresponding page in the duplicated
                    if ($duplicatedProjectPage) {
                        $duplicatedProjectPages[] = $duplicatedProjectPage->id(); // Add the new project page object to the array
                    }
                }

                $project['selection'] = $duplicatedProjectPages; // Assign the array of new project page objects
            }


        }

        $duplicatePage->update([
            'projects' => Yaml::encode($projects), // Update the projects structure field with YAML-encoded array
        ]);
    },

Thanks again!

Hi, apologies for bumping this. I’ve been trying to figure out what the issue might be but can’t figure it out. Is someone able to say, based on the code I shared, why once duplicating a page using the config function here, the structure content is duplicated too correctly, but on duplicating an item from the structure, it won’t show. Thanks agian!

What is your Kirby version?

The pages field usually stores UUIDs of the pages, not ids, so the duplication code is a bit confusing, unless you have disabled UUIDs or are using a different type of field.

Please provide the information necessary to understand what is happening here.

Also, it would be interesting to see the content of an original page that works and a duplicated one that doesn’t.

Hi Sonja,
Kirby version used is 3.5.4. If updating, should I then change id() to uuid()? Or is there something I can do while keeping this current version?

Regarding content, what happens is that if I have i.e 4 duplicated items in the structure, only the last one will display.

I see, so no uuids yet. But please, show me the content of the duplicated pages (at least the structure related content).