Skip to content
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

Sorting calendarize entries in Craft control panel #19

Closed
langlers opened this issue Mar 11, 2019 · 1 comment
Closed

Sorting calendarize entries in Craft control panel #19

langlers opened this issue Mar 11, 2019 · 1 comment

Comments

@langlers
Copy link

Do you have any advice or guidance on making entries sortable by their calendarize start date in the craft control panel? Is this a potential feature that could be built into the plugin?

I've been working off of brandonkelly's suggestion here:
craftcms/cms#2818 (comment)

@fvaldes33
Copy link
Contributor

@langlers - Not exactly as straight forward as #2818 because the calendarize data lives on a separate table. I have done something similar in the past for other projects. It would look something like this for calendarize field. The code below should work and once I test it a good amount could add it to the plugin.

Event::on(
    Entry::class, 
    Element::EVENT_REGISTER_SORT_OPTIONS, 
    function(RegisterElementSortOptionsEvent $event) {
        $event->sortOptions[] = [
            'label' => 'Calendarize | Start Date',
            'orderBy' => 'calendarize.startDate'
        ];
    }
);

Event::on(
    ElementQuery::class,
    ElementQuery::EVENT_BEFORE_PREPARE,
    function(CancelableEvent $event) {
        $query = $event->sender;
        if ($query instanceof EntryQuery) {
            if (isset($query->orderBy['calendarize.startDate'])) {
                $direction = $query->orderBy['calendarize.startDate'];
                unset($query->orderBy['calendarize.startDate']);
                $query->join[] = ['JOIN', '{{%calendarize}} calendarize', '[[entries.id]] = [[calendarize.ownerId]]'];
                $query->orderBy['calendarize.startDate'] = $direction;
            }
        }
    }
);

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

No branches or pull requests

2 participants