I am trying to get all articles based on pages field “rubriken” which is set in the article as well as inside a block to filter results.
[…]
<!-- rubriken by block -->
<?php if ($block->rubriken()->isNotEmpty()): ?>
<?php $filter2 = $block->rubriken()->toPages(); ?>
<?php endif ?>
<!-- rubriken of all articles -->
<?php if ($block->rubriken()->isNotEmpty()): ?>
<?php
$articles = $articles->filter(function($child) use($filter2) {
$kats = $child->rubriken()->toPages();
return $child->rubriken()->toPages()->has($filter2);
});
?>
<?php endif ?>
By changing the field type to “multiselect” and using “split()” instead of “toPages()” it works correctly.
But I need this as a Pages-Field to give the backend-user the ability to choose between parentpages (whole category) or subpages (subcategory).
Strange. Sorry, maybe you could have a look at this again…
<?php if ($block->rubriken()->isNotEmpty()): ?>
<!-- filter set in block -->
<?php $filter = $block->rubriken()->toPages(); ?>
<!--compare with articles -->
<?php $articles = $articles->filter(fn ($child) => $child->rubriken()->toPages()->intersects($filter) || $child->rubriken()->parents()->toPages()->intersects($filter)); ?>
<?php endif ?>
I am manually setting $filter using a page-field connected to ‘rubriken’.
If I am choosing a subpage like ‘blue’ the return works fine, but if I choose lets say the parent page ‘color’ as $filter it does not include articles having ‘blue’ in their rubriken-field although it is clearly a color.
This doesn’t make sense at all. $child->rubriken() returns a field objects, and parents() is not a method you can call on a field object.
Let’s think this through, so that you understand the problem.
In your articles’s rubriken field, you have stored the value blue which is a category that is a child of another parent category page.
The $block->rubriken(), however, returns the parent page, color. Obviously, color is not a value that is stored in your article. So rather than filtering your pages by color, you would have to pass all colors as arguments.
That means, if $filter contains a parent page like color or form, you would have to replace this value with all the children of color/form (or add them, not sure see below)
What is not quite clear to me if something like form or color will ever be stored in the articles (if so, we need the or statement, otherwise we don’t, but just pass the children)
Or your would have to get the parent pages of the already converted pages inside the filter.
What I also wonder: will $filter always contains one or more main categories like formor color or also just subcategories or even a mix?
You analyzed this correctly:
Because I don’t want the user having to also add the parent everytime he adds a subpage of a parent rubrik to his article, $filter should be able to contain a single or multiple parents selected to get only articles with its children or multiple children of multiple parents without the parents itself. (mix)
So basically you could go like: Use any form(parent) & color blue(child).
$filter = $block->rubriken()->toPages();
// make sure to pass the correct fieldname your use in your articles ass first parameter
$articles = $articles->filter(fn ($child) => $child->hasCategory('rubrik', $filter));
If my article is set to ‘round’ → filtering by ‘form’ works, but if it also has ‘blue’ (and if this blue is sorted first) it wont show up.
So for example filtering by parent ‘form’ will only show me articles, having a subpage of ‘form’ in the first place. Same behavior by using ‘color’ as $filter…
Does this return the right collection? Could you please post your page structure in the file system of the categories (with text file names visible).
This cannot possibly work, because you cannot call a field name on a collection ($articles), but only on a single page. Although I can’t tell how this variable is defined.
It helps if you always provide your variable definitions, otherwise it’s just guesswork.
Hej I am getting this message: Block error: "You must pass a Pages or Page object or an ID of an existing page to the Pages collection" in block type: "grid"
Maybe change it to “toPages()”?
$allUsedCategories = new Pages();
$fields = $articles->pluck('rubriken', null));
foreach($fields as $field) {
$cats = $field->toPages();
$allUsedCategories->add($cats);
}
The only (and I swear last) problem is, that this is giving me every used category of $articles. I would like to narrow this down more to only the ones which are also selected via $filter at the beginning:
<?php $filter = $block->rubriken()->toPages(); ?>
////
That’s why I have tried to use another filter() on $allUsedCategories
<!-- Filter by Block again -->
<?php $allUsedCategories = $allUsedCategories->filter(fn ($child) => $child->has($filter)); ?>
<?php var_dump($filter); ?><br>
<?php var_dump($allUsedCategories); ?>