How can I merge structure fields in K3 ? I used this recipe for K2:
Merging structure fields
Thanks a lot
How can I merge structure fields in K3 ? I used this recipe for K2:
Merging structure fields
Thanks a lot
Doesn’t the K2 way of doing this work anymore? I’ll try to look into this tonight.
Try renaming /site/plugins/structure.php
to /site/plugins/structure/index.php
and put this inside:
<?php
use Kirby\Cms\Structure;
function createNewStructure($pages, $field)
{
// instantiate a new structure object
$structure = new Structure();
// loop through the pages collection
foreach ($pages as $p) {
$structure->add($p->$field()->toStructure());
}
return $structure;
}
Great, this works !
Couldn’t figure out how to start the plugin.
Is there also a way to skip identical entries in the new structure field ?
I have subpages where I put people in structure fields (first name, last name, website).
Then on a different page I show a list of all persons – now they are listed multiple times if they appear on multiple subpages.
To skip identical entries when structure fields where merged. Probably there are much more elegant ways to do it but it worked for me:
<?php foreach($page->structurefield()->toStructure()->sortBy('firstname', 'asc') as $person): ?>
<?php $firstname = $person->firstname()->toString(); ?>
<?php $lastname = $person->lastname()->toString(); ?>
<?php $website = $person->website()->toString(); ?>
<?php $entry[] = $firstname . ', ' . $lastname . ', ' . $website; ?>
<?php endforeach ?>
<?php $row = array_keys(array_flip($entry)); ?>
<?php foreach($row as $r): ?>
<?php echo $r ?><br />
<?php endforeach ?>