diff --git a/CHANGELOG.md b/CHANGELOG.md index 22fc74d..1b8a9f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Release Notes for Entry Meta +## 1.0.0-rc.4 - 2021-12-01 +### Added +- Convert keys and values displayed on sidebar to human-readable format. + +### Changed +- Refactored plugin class + ## 1.0.0-rc.3 - 2021-11-29 ### Changed - Breaking Change: Remove query service. Use ActiveQuery instead for more flexible queries. diff --git a/src/EntryMeta.php b/src/EntryMeta.php index d9fc03d..b08613b 100644 --- a/src/EntryMeta.php +++ b/src/EntryMeta.php @@ -11,6 +11,7 @@ use matfish\EntryMeta\behaviors\EntryActiveQueryBehavior; use matfish\EntryMeta\behaviors\EntryElementBehavior; use matfish\EntryMeta\models\Settings; +use matfish\EntryMeta\twig\EntryMetaExtension; use yii\base\Event; use craft\records\Entry as EntryRecord; @@ -23,15 +24,30 @@ public function init() { parent::init(); + $this->registerElementBehavior(); + $this->registerQueryBehavior(); + + if (Craft::$app->request->getIsCpRequest() && $this->settings->displayMetadataInCp) { + $this->registerCpMetaHook(); + } + } + + protected function createSettingsModel() + { + return new Settings(); + } + + private function registerElementBehavior() + { Event::on(ActiveQuery::class, ActiveQuery::EVENT_INIT, function ($e) { if ($e->sender->modelClass === EntryRecord::class) { $e->sender->attachBehaviors([EntryActiveQueryBehavior::class]); } }); + } - /** - * Attach a behavior after an entry has been loaded from the database (populated). - */ + private function registerQueryBehavior() + { Event::on(ElementQuery::class, ElementQuery::EVENT_AFTER_POPULATE_ELEMENT, function (PopulateElementEvent $event) { $element = $event->element; @@ -40,22 +56,19 @@ public function init() } }); - if ($this->settings->displayMetadataInCp) { - Craft::$app->getView()->hook(self::CP_HOOK, function (array &$context) { - - $entry = $context['entry']; - $meta = $entry->getEntryMetadata(); - - return Craft::$app->view->renderTemplate('entry-meta/_meta', [ - 'data' => $meta - ]); - }); - } } - protected function createSettingsModel() + private function registerCpMetaHook() { - return new Settings(); - } + Craft::$app->view->registerTwigExtension(new EntryMetaExtension()); + + Craft::$app->getView()->hook(self::CP_HOOK, function (array &$context) { + $entry = $context['entry']; + $meta = $entry->getEntryMetadata(); + return Craft::$app->view->renderTemplate('entry-meta/_meta', [ + 'data' => $meta + ]); + }); + } } \ No newline at end of file diff --git a/src/templates/_meta.twig b/src/templates/_meta.twig index 15dec4f..7392eca 100644 --- a/src/templates/_meta.twig +++ b/src/templates/_meta.twig @@ -1,8 +1,8 @@ {% for key,value in data %}