Created
September 29, 2022 16:29
-
-
Save markhuot/7331d32d1ef16473d3d56e68a99f09a1 to your computer and use it in GitHub Desktop.
Craft Behaviors for Entry Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% set entries = craft.entries.popular().author(currentUser).limit(10) %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules\behaviors; | |
use yii\base\Behavior; | |
class EntryQuery extends Behavior | |
{ | |
function popular() | |
{ | |
$query = $this->owner; | |
$query->isPopular(true); | |
$query->postDate('>= ' . (new \DateTime('-1 week'))->format('Y-m-d')); | |
return $query; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace modules\behaviors; | |
use craft\elements\db\EntryQuery; | |
class Module extends \yii\base\Module | |
{ | |
function init() | |
{ | |
Event::on(EntryQuery::class, EntryQuery::EVENT_DEFINE_BEHAVIORS, function ($event) { | |
$event->behaviors['_site_entry_query'] = \modules\behaviors\EntryQueryBehavior::class; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment