Categories from parent page and structure

Hi, I create categories from parent page with structure:

      category:
        label: Categories
        type: structure                
        fields:
          cattitle:
            label: Title
            type: text
          catdesc:
            label: Content
            type: textarea

In subpage, I select categories with checkboxes:

              cat:
                label: Category
                type: checkboxes
                #style: table
                options: query
                query: 
                  fetch: page.parent.category.toStructure
                  text: "{{ structureItem.cattitle }}"
                  value: "{{ structureItem.cattitle }}"

In the parent page template, I show the categories and correspondant subpages this way:

  <?php $subpages = $page->children()->listed() ;?>
  <?php $categories  = $subpages->sortBy('cat', 'sort')->pluck('cat', ',', true);?>
  <?php foreach ($categories as $category): ?>
  <div class="">
    <h2><?php echo html($category) ?></h2>
    <?php $catlist = $subpages->filterBy('cat', $category, true); ?>
    <?php foreach ($catlist as $cat): ?>
    <a <?= r($cat->isOpen(), ' class="is-active"') ?> href="<?php echo $cat->url() ?>"><?php echo $cat->title()->html() ?></a>
    <?php endforeach ?>
  </div>
  <?php endforeach ?>

It seems to work corectly, but I can’t sort categories manually. Only asc or desc will work.

So I tried something like this, but does not work, categories titles from structures are correctly shown, but I can’t output nothing from correspondant subpages :frowning: and I’ve no idea how to do it.

<?php foreach ($page->category()->toStructure() as $category): ?>
  <h2><?= $category->cattitle()->html() ?></h2>  
  <?php $categorylist = $page->children()->filterBy('cat', $category) ?>
  <?php foreach ($categorylist as $cat): ?>
  <a<?= r($cat->isOpen(), ' class="is-active"') ?> href="<?php echo $cat->url() ?>"><?php echo $cat->title()->html() ?></a>
  <?php endforeach ?>
  <?php endforeach ?>

Any help is welcome, thank you.

What do you mean by “sort categories manually”? Do you want to preserve the same order as in the structure field?

The problem is this line:

 <?php $categorylist = $page->children()->filterBy('cat', $category) ?>

You can filter by the structure item, but have to use the value you stored in your subpage, in this case the title, so it should become:

 <?php $categorylist = $page->children()->filterBy('cat', $category->title() ?>

Yes, exactly, I would to be able to keep order from the structure field. I’m sur user will ask for it at a moment…

I also tried

 <?php $categorylist = $page->children()->filterBy('cat', $category) ?>

Just output nothing

 <?php $categorylist = $page->children()->filterBy('cat', $category->cattitle()) ?>

If multiple categories are selected, it outputs only one or nothing

You would have to add the separator as well for multiple values

 <?php $categorylist = $page->children()->filterBy('cat', $category->cattitle(), ',') ?>

Yes, it was obvious, I should have thought about it! thanks again Sonja.

If you want to prevent outputting categories that have no entries, you could check if the given value is actually used:

<?php 
$usedCategories = $page->children()->pluck('cat', ',', true);
foreach ($page->category()->toStructure() as $category): ?>
  <?php if (!in_array($category->cattitle(), $usedCategories)) continue; ?>
  <h2><?= $category->cattitle()->html() ?></h2>  
  <?php $categorylist = $page->children()->filterBy('cat', $category-> cattitle(), ',') ?>
  <?php foreach ($categorylist as $cat): ?>
    <a<?= r($cat->isOpen(), ' class="is-active"') ?> href="<?php echo $cat->url() ?>"><?php echo $cat->title()->html() ?></a>
  <?php endforeach ?>
<?php endforeach ?>
1 Like

Is there a camera next to my computer ?? I was just working on this :wink: and was wondering if it was a good practice to let an empty category visible…
Thanks a lot for the tips and code. I just “correct” it with a missing close parenthesis:
<?php if(!in_array($category->cattitle(), $usedCategories)) continue; ?>