Skip to content

Persistent/default filters #13499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2023

Conversation

brandonkelly
Copy link
Member

@brandonkelly brandonkelly commented Aug 2, 2023

Description

Element indexes now store the serialized filter condition in the query string, so the current results can be shared with others, and filters will persist when navigating back to an element index via browser history.

This also adds support for native sources to define a defaultFilter key, which can be set to an element condition instance or config, which defines the default filter condition that should be applied when the source is first selected (per page load). The user will still have the ability to modify or remove it after that.

use craft\base\Event;
use craft\elements\conditions\entries\TypeConditionRule;
use craft\elements\Entry;
use craft\events\RegisterElementSourcesEvent;
use craft\helpers\ArrayHelper;
use craft\models\EntryType;

Event::on(Entry::class, Entry::EVENT_REGISTER_SOURCES, function(RegisterElementSourcesEvent $event) {
    $newsSection = Craft::$app->getSections()->getSectionByHandle('news');
    /** @var EntryType $articleType */
    $articleType = ArrayHelper::firstWhere(
        $newsSection->getEntryTypes(),
        fn(EntryType $type) => $type->handle === 'article'
    );
    $condition = Entry::createCondition();
    $condition->setConditionRules([
        new TypeConditionRule([
            'values' => [$articleType->uid],
        ]),
    ]);

    $event->sources[] = [
        'key' => 'articles',
        'label' => 'Articles',
        'criteria' => ['section' => 'news'],
        'defaultFilter' => $condition,
    ];
});

Related issues

@linear
Copy link

linear bot commented Aug 2, 2023

[ci skip]
@brandonkelly brandonkelly merged commit 88854b0 into 4.5 Aug 2, 2023
@brandonkelly brandonkelly deleted the feature/dev-652-persistent-element-index-filters branch August 2, 2023 22:39
@myleshyson
Copy link
Contributor

myleshyson commented Aug 3, 2023

The default filter doesn't seem to persist when selecting a new source. It's only applied on the first page load for the initial source that's selected.

Screen.Recording.2023-08-03.at.9.56.27.AM.mov

Also noticed that I had to use class array syntax when defining defaultFilter on nested sources, otherwise the elementIndex twig template would error out. So

// this works for nested sources
'defaultFilter' => [
            'class' => EventCondition::class,
            'conditionRules' => [
                [
                    'class' => StartDateConditionRule::class,
                    'rangeType' => StartDateConditionRule::OPERATOR_NEXT
                ]
            ]
        ]

// this errors out for nested sources
$condition = Event::createCondition();
$condition->setConditionRules([
new StartDateCondition(['rangeType' => StartDateConditionRule::OPERATOR_NEXT ])
])
'defaultFilter' => $condition

brandonkelly added a commit that referenced this pull request Aug 3, 2023
@brandonkelly
Copy link
Member Author

@myleshyson Thanks for testing! Just fixed both of those (37ec494).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants